Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

ex9 & 10 python

Uploaded by

tckgaming38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ex9 & 10 python

Uploaded by

tckgaming38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

9.

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

1. Define the Function:

 Create a function named copy_file_content that takes two


parameters: input_file (the name of the file to read from) and output_file (the name
of the file to write to).

2. Open the Input File:

 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.

3. Read the Content:

 Use the read() method to read the entire content of the input file and store it in a
variable named content.

4. Open the Output File:

 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.

5. Write the Content:

 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:

 FileNotFoundError: This occurs if the input file does not exist.

 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.

 Print error messages if any exceptions are caught.

8. Specify File Names:

 Define the names of the input and output files (e.g., input.txt and output.txt).

9. Call the Function:

 Invoke the copy_file_content function with the specified input and output file
names.

Program

def copy_file_content(input_file, output_file):

try:

# Open the input file in read mode

with open(input_file, 'r') as infile:

# Read the content of the input file

content = infile.read()

# Open the output file in write mode

with open(output_file, 'w') as outfile:

# Write the content to the output file

outfile.write(content)

print(f"Content copied from {input_file} to {output_file} successfully.")

except FileNotFoundError:

print(f"Error: The file {input_file} does not exist.")

except Exception as e:

print(f"An error occurred: {e}")

# Specify the input and output file names

input_file = 'input.txt'
output_file = 'output.txt'

# Call the function to copy content

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

1. Import the Pandas Library:

 Start by importing the pandas library, which provides powerful data


manipulation and analysis tools.

2. Define the Function:

 Create a function named read_csv_file that takes one parameter: file_path,


which is the path to the CSV file to be read.

3. Read the CSV File:

 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.

5. Call the Function:

 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:

You might also like