Python File Operations
Path Types
Relative paths reference files in relation to the current working directory. A file in the same directory is referenced by its name. To access a parent directory, use ../. Absolute paths specify the full route from the root directory to the file. Relative paths are preferred for portability, as they allow projects to be moved without breaking references.
File Access Fundamentals
The open() function is used to access a file, returning a file object for subsequent operations. The mode parameter determines the allowed actions.
Available modes include: r (read), w (write), a (append), r+ (read/write), w+ (write/read), a+ (append/read), and their binary counterparts (rb, wb, etc.). The default mode is r.
Reading Files (r, rb)
The r mode is for text files, requiring an encoding specification. The rb mode reads binary data, such as images or audio, and does not accept an encoding argument.
Reading Entire Content
with open('champions_list.txt', mode='r', encoding='utf-8') as file_handle:
data = file_handle.read()
print(data)
Reading the entire file at once can consume significant memory for large files.
Reading a Specific Number of Characters
with open('champions_list.txt', mode='r', encoding='utf-8') as file_handle:
part1 = file_handle.read(4)
part2 = file_handle.read(5)
print(part1)
print(part2)
In binary mode, read(n) retrieves n bytes.
Reading Line by Line
with open('champions_list.txt', mode='r', encoding='utf-8') as file_handle:
line = file_handle.readline()
print(line.strip()) # strip() removes trailing newline
Reading All Lines into a List
with open('champions_list.txt', mode='r', encoding='utf-8') as file_handle:
lines_list = file_handle.readlines()
print(lines_list)
This method also poses a risk of high memory usage.
Iterative Reading
with open('champions_list.txt', mode='r', encoding='utf-8') as file_handle:
for line in file_handle:
print(line, end='')
Iterating over the file object is memory-efficient.
Writing Files (w, wb)
The w mode creates a new file or overwrites an existing one. wb writes bytes and requires encoding conversion.
with open('warrior_profile.txt', mode='w', encoding='utf-8') as profile:
profile.write("A disciplined fighter who has mastered the art of the blade.\n")
profile.write("His calm demeanor hides a fiery spirit ready for battle.\n")
with open('warrior_profile.txt', mode='rb') as profile_bin:
content_bytes = profile_bin.read()
Appending to Files (a, ab)
The a mode adds content to the end of a file without altering existing data.
with open('warrior_profile.txt', mode='a', encoding='utf-8') as profile:
profile.write("He hails from the northern mountains.\n")
Combined Read and Write Modes
Read-Write (r+)
Allows reading and writing. The pointer starts at the beginning. Its crucial to read before writing to position the pointer correctly.
with open('warrior_profile.txt', mode='r+', encoding='utf-8') as profile:
print(profile.read())
profile.write("Origin: Northern Mountains")
Write-Read (w+)
Truncates the file and writes new content. Reading immediately after writing may yield no data.
with open('temp_data.txt', mode='w+', encoding='utf-8') as temp_file:
temp_file.write("Temporary information")
temp_file.seek(0)
print(temp_file.read())
Append-Read (a+)
Appends data. The read pointer is at the end, so reading requires seeking to the start.
with open('log.txt', mode='a+', encoding='utf-8') as log_file:
log_file.seek(0)
print(log_file.read())
log_file.write("New event recorded.\n")
Advanced File Manipulation
seek(offset, whence): Moves the file pointer.offsetis the number of bytes.whencedefines the reference point (0 for start, 1 for current, 2 for end).tell(): Returns the current pointer position.truncate(size): Truncates the file. Ifsizeis omitted, it truncates from the current position.
with open('data.txt', 'r+', encoding='utf-8') as file:
file.seek(10)
file.write('INSERTED TEXT')
file.truncate()
Modifying File Content
To modify a file, read its content, make changes in memory, and write to a new file. Then, replace the old file.
import os
with open('source.txt', 'r', encoding='utf-8') as source, \
open('temp_modified.txt', 'w', encoding='utf-8') as target:
for line in source:
modified_line = line.replace('old_value', 'new_value')
target.write(modified_line)
os.remove('source.txt')
os.rename('temp_modified.txt', 'source.txt')
Processing line by line prevents excessive memory consumption.