Python's built-in open() function is the primary tool for interacting with files stored on disk. It requires a file path and a mode string to specify the operation type.
File Opening Modes
The mode parameter defines how the file stream is accessed:
'r': Read-only mode (default). Raises an error if the file does not exist.'w': Write mode. Truncates the file first if it exists, or creates a new one.'a': Append mode. Writes data to the end of the file without clearing existing content.'x': Exclusive creation. Fails if the file already exists.'b': Binary mode (e.g.,'rb'for reading binary files).'t': Text mode (default).
Reading Content
Loading Entire Content
The .read() method fetches the full content as a single string. Be cautious with large files to avoid high memory usage.
data_stream = open('sample.txt', 'r')
full_text = data_stream.read()
print(full_text)
data_stream.close()
Line-by-Line Processing
For large datasets, reading line by line is more memory-efficient. The .readline() method fetches one line per call.
handle = open('sample.txt', 'r')
current_line = handle.readline()
while current_line:
print(current_line.strip())
current_line = handle.readline()
handle.close()
Storing Lines in a List
The .readlines() method parses the entire file and returns a list where each element is a line.
handle = open('sample.txt', 'r')
line_list = handle.readlines()
for entry in line_list:
print(entry.strip())
handle.close()
Context Managers (The with Statement)
Using a with block is the standard approach for file handling. It guarantees that the file descriptor is released automatically after the block executes, evenif errors occur.
with open('sample.txt', 'r') as file_handle:
file_content = file_handle.read()
print(file_content)
Error Handling
Wrap file operations in try-except blocks to manage common issues like missing files or permission errors.
try:
with open('sample.txt', 'r') as file_handle:
print(file_handle.read())
except FileNotFoundError:
print("Error: The specified file could not be located.")
except IOError:
print("Error: The file could not be accessed due to an I/O issue.")
except Exception as err:
print(f"An unexpected error occurred: {err}")
Character Encoding
Mismatched encodings can lead to UnicodeDecodeError. Explicitly set the encoding parameter when opening text files.
with open('sample.txt', 'r', encoding='utf-8') as file_handle:
print(file_handle.read())
Efficient Iteration for Large Files
The most Pythonic way to process a large file is iterating directly over the file object within a with block. This streams the file line by line.
with open('sample.txt', 'r', encoding='utf-8') as file_handle:
for row in file_handle:
print(row.strip())