ex9 & 10 python
ex9 & 10 python
Aim
The aim of the program is to read the content from a specified text file and write that content to
another text file. This can be useful for various purposes, such as creating backups, duplicating
files, or processing text data.
Procedure
Use a with statement to open the input file in read mode ('r'). This ensures that the
file is properly closed after its contents are read, even if an error occurs.
Use the read() method to read the entire content of the input file and store it in a
variable named content.
Again, use a with statement to open the output file in write mode ('w'). If the file
does not exist, it will be created. If it does exist, its contents will be overwritten.
Use the write() method to write the content read from the input file into the output
file.
6. Error Handling:
Implement error handling using try and except blocks to catch potential exceptions,
such as:
General exceptions: Any other errors that may arise during file operations.
7. User Feedback:
Print a success message indicating that the content has been copied successfully.
Define the names of the input and output files (e.g., input.txt and output.txt).
Invoke the copy_file_content function with the specified input and output file
names.
Program
try:
content = infile.read()
outfile.write(content)
except FileNotFoundError:
except Exception as e:
input_file = 'input.txt'
output_file = 'output.txt'
copy_file_content(input_file, output_file)
10) Aim
The aim of the program is to read a CSV (Comma-Separated Values) file using
the pandas library in Python and print its content to the console. This is useful for data analysis,
data manipulation, and visualization tasks, as CSV files are a common format for storing tabular
data.
Procedure
Inside the function, use pd.read_csv(file_path) to read the CSV file into a
DataFrame. A DataFrame is a two-dimensional, size-mutable, and potentially
heterogeneous tabular data structure with labeled axes (rows and columns).
4. Print the Content:
Use the print() function to display the content of the DataFrame, which
represents the data from the CSV file in a tabular format. Specify the CSV
File Path:
Define the path to the CSV file (e.g., data.csv). This can be a relative path (if
the file is in the same directory as the script) or an absolute path.
Invoke the read_csv_file function with the specified CSV file path to execute
the reading and printing of the file content.
Program
#import pandas
Import pandas as pd
# import dataset
df=pd.read_csv(r"C:\Users\Sureshkumar\Desktop\de&v\chitpole.csv")
print(df)
Output: