Python > Core Python Basics > Input and Output > File Handling (opening, reading, writing, closing files)
Simple File Reading and Writing
This snippet demonstrates basic file operations: opening a file, writing data to it, reading data from it, and closing the file. It covers the fundamental 'open', 'write', 'read', and 'close' functions.
Basic File Writing
This part opens a file named 'my_file.txt' in write mode ('w'). The 'w' mode overwrites the file if it already exists. The 'write()' method writes strings to the file. The 'flush()' method ensures the data is written to disk immediately. The 'finally' block ensures the file is closed, even if an exception occurs during writing, to prevent resource leaks.
filename = 'my_file.txt'
try:
# Open the file in write mode ('w'). This will overwrite the file if it exists.
file = open(filename, 'w')
# Write some data to the file.
file.write('Hello, world!\n')
file.write('This is a test file.\n')
# Ensure all data is written to disk.
file.flush()
except Exception as e:
print(f'An error occurred: {e}')
finally:
# Close the file to release resources, even if errors occurred.
if 'file' in locals() and not file.closed:
file.close()
print('File closed.')
print('Writing completed (or attempted).')
Basic File Reading
This part opens the same file in read mode ('r'). The 'read()' method reads the entire contents of the file as a single string. Error handling is included to catch 'FileNotFoundError' if the file doesn't exist. Again, the 'finally' block ensures the file is closed.
filename = 'my_file.txt'
try:
# Open the file in read mode ('r')
file = open(filename, 'r')
# Read the entire content of the file.
content = file.read()
# Print the content to the console.
print(content)
except FileNotFoundError:
print(f'File not found: {filename}')
except Exception as e:
print(f'An error occurred: {e}')
finally:
# Close the file, even if errors occurred.
if 'file' in locals() and not file.closed:
file.close()
print('File closed.')
print('Reading completed (or attempted).')
Concepts Behind the Snippet
This snippet demonstrates the core concepts of file handling:
open()
function with the filename and mode (e.g., 'r' for read, 'w' for write).read()
method to read the file's contents.write()
method to write data to the file.close()
method to release resources. It's crucial to always close files after use.try...except...finally
blocks to handle potential errors (e.g., file not found, permission errors) and ensure proper file closure.
Real-Life Use Case
This basic file handling is used in many applications, such as:
Best Practices
finally
block or the with
statement (demonstrated in the next example) to ensure files are closed, even if errors occur.try...except
blocks to catch potential errors and prevent your program from crashing.encoding
parameter of the open()
function.
Interview Tip
When asked about file handling, be sure to mention the importance of closing files to prevent resource leaks. Also, discuss the various file modes and when each one should be used. Demonstrate knowledge of error handling techniques. Mention using the 'with' statement for automatic resource management.
When to use them
These techniques are best for simple file operations where performance is not critical. For large files or complex operations, consider using more advanced techniques like buffering or memory mapping.
Memory footprint
The read()
method loads the entire file into memory at once. This can be problematic for very large files. For large files, use techniques like reading the file line by line or using libraries that support streaming data.
Alternatives
Instead of file.read()
, one can use file.readline()
to read a file line by line, or file.readlines()
to get a list of all lines. For more complex data structures, libraries like csv
(for comma-separated values) or json
(for JSON data) are often used.
Pros
Cons
read()
loads the entire file into memory.
FAQ
-
What happens if I try to open a file in read mode that doesn't exist?
AFileNotFoundError
exception will be raised. You should handle this exception in atry...except
block. -
What is the difference between 'w' and 'a' modes?
The 'w' mode overwrites the file if it exists, while the 'a' mode appends to the end of the file. If the file doesn't exist, both modes will create a new file. -
Why is it important to close files?
Closing files releases the resources held by the operating system. If you don't close files, you may encounter issues like resource leaks, data corruption, or the inability to access the file later.