0% found this document useful (0 votes)
25 views3 pages

Practical No 9

Uploaded by

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

Practical No 9

Uploaded by

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

PIMPRICHINCHWADUNIVERSITY

SchoolofEngineeringandTechnology

AcademicYear:2024-2025 Sem:III
Course: PythonProgramming Coursecode:UBTCE203/PCC
Name: RollNo:
Practical No. 9
Title: To write a python program to perform file manipulations- open, close, read, write, append and
copy from one file to another

Objective:
The objective of a Python program designed to perform file manipulations, including opening, closing,
reading, writing, appending, and copying content between files, is to provide a comprehensive
understanding and practical application of basic file operations in Python.

Outcomes:
At the end of this exercise, the student will be able to:

1. Open and Close Files: Efficiently open files in various modes (such as read, write, append)
and properly close them after completing operations to ensure data integrity and resource
management.
2. Read File Content: Read and extract content from files, whether it's reading the entire file,
specific lines, or portions of the file, and handle this data for further processing.
3. Write to Files: Write new data to files, including overwriting existing content and ensuring
that the file is correctly updated with the new information.
4. Append to Files: Add additional content to the end of an existing file without modifying or
deleting the current contents, enabling cumulative data storage.
5. Copy Files: Copy content from one file to another, understanding how to manage file paths
and handle the file copy process accurately.
6. Handle Errors: Apply error handling techniques to manage common file operation issues,
such as missing files or permission errors, ensuring that the program runs smoothly even in
unexpected situations.

Aim: To write a python program to perform file manipulations- open, close, read, write, append and
copy from one file to another

.Theory:
To understand the program, one should be familiar with basic file operations in Python, such as opening
files with open(), and handling read, write, and append modes. It’s crucial to ensure files are properly
closed after operations to avoid resource leaks. Managing file paths and handling errors, such as missing
files or permission issues, is also important for robust file manipulation
PIMPRICHINCHWADUNIVERSITY
SchoolofEngineeringandTechnology

AcademicYear:2024-2025 Sem:III
Course: PythonProgramming Coursecode:UBTCE203/PCC
Name: RollNo:

Algorithm
START
• Open a File
• Read from file
• Write to file
• Append to file
• Copy File
• Close the file
END

SourceCode:
import shutil

# Define file paths


source_file = 'source.txt'
destination_file = 'destination.txt'
append_file = 'append_file.txt'

# 1. Open, Read, and Write to a File


try:
# Open file in write mode (creates a new file or overwrites existing)
with open(source_file, 'w') as file:
file.write('This is the original content.\n')
file.write('Adding some more text.')

# Open file in read mode


with open(source_file, 'r') as file:
content = file.read()
print("Read content from source file:")
print(content)

except IOError as e:
print(f"Error opening/reading file: {e}")

# 2. Append to a File
try:
# Open file in append mode (adds to the end of the file)
with open(source_file, 'a') as file:
file.write('\nAppending new content.')

# Verify appending by reading the file again


with open(source_file, 'r') as file:
content = file.read()
print("\nContent after appending:")
print(content)
PIMPRICHINCHWADUNIVERSITY
SchoolofEngineeringandTechnology

AcademicYear:2024-2025 Sem:III
Course: PythonProgramming Coursecode:UBTCE203/PCC
Name: RollNo:

except IOError as e:
print(f"Error opening/reading/appending file: {e}")

# 3. Copy Content from One File to Another


try:
# Copy the content from source_file to destination_file
shutil.copy(source_file, destination_file)
print(f"\nFile copied from {source_file} to {destination_file}.")

# Verify copy by reading the destination file


with open(destination_file, 'r') as file:
content = file.read()
print("\nContent of the destination file:")
print(content)

except IOError as e:
print(f"Error copying file: {e}")

Output:
After Writing and Reading from the Source File:
Read content from source file:
This is the original content.
Adding some more text.

After Appending to the Source File:


Content after appending:
This is the original content.
Adding some more text.
Appending new content.

After Copying the Source File to the Destination File:

File copied from source.txt to destination.txt.


Content of the destination file:
This is the original content.
Adding some more text.
Appending new content.

Conclusion:

Thus we implemented File Handling in Python

You might also like