Practical No 9
Practical No 9
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
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.')
AcademicYear:2024-2025 Sem:III
Course: PythonProgramming Coursecode:UBTCE203/PCC
Name: RollNo:
except IOError as e:
print(f"Error opening/reading/appending file: {e}")
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.
Conclusion: