Python > Core Python Basics > Input and Output > File Handling (opening, reading, writing, closing files)
File Handling with 'with' Statement and Line-by-Line Reading
This snippet demonstrates using the 'with' statement for automatic resource management and reading a file line by line. The 'with' statement ensures the file is automatically closed, even if errors occur. Reading line by line is more memory-efficient for large files.
Writing to a File using 'with'
This code opens 'my_file.txt' in write mode using the with
statement. The file is automatically closed when the with
block finishes, regardless of whether an exception occurs. This ensures proper resource management.
filename = 'my_file.txt'
try:
# The 'with' statement automatically closes the file when the block is exited.
with open(filename, 'w') as file:
file.write('First line\n')
file.write('Second line\n')
file.write('Third line\n')
print('Writing completed.')
except Exception as e:
print(f'An error occurred: {e}')
Reading a File Line by Line using 'with'
This code opens 'my_file.txt' in read mode using the with
statement. It then iterates through the file line by line using a for
loop. The line.strip()
method removes any leading or trailing whitespace (including the newline character) from each line before printing it.
filename = 'my_file.txt'
try:
# Open the file in read mode using the 'with' statement.
with open(filename, 'r') as file:
# Iterate through each line in the file.
for line in file:
# Print each line to the console (removing the trailing newline character).
print(line.strip())
print('Reading completed.')
except FileNotFoundError:
print(f'File not found: {filename}')
except Exception as e:
print(f'An error occurred: {e}')
Concepts Behind the Snippet
This snippet illustrates:
with
statement: This provides a clean and reliable way to manage resources like files. It automatically closes the file when the block is exited, even if exceptions occur.
Real-Life Use Case
Reading line by line is especially useful for:
Best Practices
with
statement for file handling: It ensures automatic resource management and prevents resource leaks.strip()
to remove whitespace from lines.csv
or json
for structured data: If your file contains structured data, using these libraries can simplify parsing and processing.
Interview Tip
Highlight your understanding of the with
statement and its benefits for resource management. Emphasize the importance of memory efficiency when dealing with large files and the advantages of reading line by line. Be prepared to discuss the differences between read()
, readline()
, and readlines()
.
When to use them
The 'with' statement should always be preferred for file handling. Reading files line by line should be preferred when you deal with large files to save memory space.
Memory footprint
Reading a file line by line keeps only one line in memory at a time, which makes it highly memory-efficient, especially when compared to file.read()
which loads the entire file into memory. It is the preferred approach for large files.
Alternatives
An alternative is to use the readlines()
method, which reads all lines into a list. However, for truly massive files, reading line by line within a loop is generally preferable due to lower memory usage. For specialized file formats, using parsing libraries (e.g., CSV, JSON, XML) might be more appropriate.
Pros
Cons
FAQ
-
What are the benefits of using the 'with' statement?
The 'with' statement guarantees that the file will be closed properly, even if errors occur. It simplifies resource management and makes your code more robust. -
How can I read only a certain number of lines from a file?
You can use a loop with a counter to read only the first N lines of a file. For example: python with open('my_file.txt', 'r') as file: for i, line in enumerate(file): print(line.strip()) if i == 9: # Read only the first 10 lines break -
How can I handle different character encodings when reading a file?
You can specify the character encoding when opening the file using theencoding
parameter of theopen()
function. For example, to read a file encoded in UTF-8, use:with open('my_file.txt', 'r', encoding='utf-8') as file: