File Handling
File Handling
opening, reading, writing and closing it, through a programming interface. It involves
managing the data flow between the program and the file system on the storage device,
ensuring that data is handled safely and efficiently.
To open a file we can use open() function, which requires file path and mode as arguments:
When opening a file, we must specify the mode we want to which specifies what we want to
do with the file. Here’s a table of the different modes available:
Mod
e Description Behavior
a+ Append and read mode. Opens the file for appending and reading.
Append and read in binary Opens the file for appending and reading binary
ab+ mode. data. Creates a new file if it doesn’t exist.
Reading a file can be achieved by file.read() which reads the entire content of the file. After
reading the file we can close the file using file.close() which closes the file after reading it,
which is necessary to free up system resources.
content = file.read()
print(content)
file.close()
Output:
Hello world
Good Luck
123 456
content = file.read()
print(content)
file.close()
Output:
Writing to a File
Writing to a file is done using file.write() which writes the specified string to the file. If the
file exists, its content is erased. If it doesn’t exist, a new file is created.
file.write("Hello, World!")
file.close()
Example: For this example, we will use the Python file created in the previous example.
file.close()
Closing a File
Closing a file is essential to ensure that all resources used by the file are properly
released. file.close() method closes the file and ensures that any changes made to the file
are saved.
file.close()
with statement is used for resource management. It ensures that file is properly closed after
its suite finishes, even if an exception is raised. with open() as method automatically handles
closing the file once the block of code is exited, even if an error occurs. This reduces the risk
of file corruption and resource leakage.
content = file.read()
print(content)
Output:
Hello, World!
Appended text.
It’s important to handle exceptions to ensure that files are closed properly, even if an error
occurs during file operations.
try:
print(content)
except FileNotFoundError:
finally:
file.close()
Output:
Hello, World!
Appended text.
User – friendly : Python provides a user-friendly interface for file handling, making it
easy to create, read and manipulate files.
Security risks : File handling in Python can also pose security risks, especially if the
program accepts user input that can be used to access or modify sensitive files on
the system.
Complexity : File handling in Python can be complex, especially when working with
more advanced file formats or operations. Careful attention must be paid to the code
to ensure that files are handled properly and securely.
Performance : File handling operations in Python can be slower than other
programming languages, especially when dealing with large files or performing
complex operations.