Text files (TXT) are a fundamental form of data storage, often used for logs, configuration settings, or simply storing textual information. Python, with its elegant syntax and robust standard library, offers several ways to modify TXT files efficiently. In this article, we’ll explore the various methods of modifying TXT files with Python, including reading, writing, appending, and even searching and replacing text within files.
Reading TXT Files
Before you can modify a TXT file, you first need to read its contents. Python’s built-in open()
function, combined with file methods like read()
, readline()
, and readlines()
, makes this task straightforward.
python# Read the entire file
with open('example.txt', 'r') as file:
content = file.read()
# Read one line at a time
with open('example.txt', 'r') as file:
for line in file:
print(line.strip()) # strip() removes newline characters
# Read all lines into a list
with open('example.txt', 'r') as file:
lines = file.readlines()
Writing and Overwriting TXT Files

To modify the contents of a TXT file, you typically need to write new content back to the file. Be aware that using write mode ('w'
) will overwrite the entire file, so it’s essential to ensure you have a backup or are ready to lose the existing data.
python# Overwrite the entire file
with open('example.txt', 'w') as file:
file.write("This is the new content of the file.")
Appending to TXT Files

If you want to add new content to the end of a file without overwriting existing data, you can use append mode ('a'
).
python# Append to the file
with open('example.txt', 'a') as file:
file.write("\nThis is a new line appended to the file.")
Searching and Replacing Text in TXT Files

Searching and replacing text within a TXT file requires reading the file’s contents, performing the search and replace operation, and then writing the modified content back to the file.
python# Read the file, replace text, and write back
with open('example.txt', 'r') as file:
content = file.read()
# Replace 'old_text' with 'new_text'
content = content.replace('old_text', 'new_text')
with open('example.txt', 'w') as file:
file.write(content)
Handling Large Files

When working with large TXT files, it’s essential to avoid reading the entire file into memory at once. Instead, you can read and write the file in chunks or process one line at a time.
python# Modify large files by processing one line at a time
with open('example.txt', 'r') as read_file, open('modified_example.txt', 'w') as write_file:
for line in read_file:
# Perform any necessary modifications to the line
modified_line = line.replace('old_text', 'new_text')
write_file.write(modified_line)
Conclusion

Modifying TXT files with Python is a straightforward process that involves reading, writing, appending, and potentially searching and replacing text within files. By leveraging Python’s built-in open()
function and file methods, you can easily manipulate TXT files to meet your needs. Whether you’re working with small or large files, Python provides powerful tools to help you get the job done efficiently.
Python official website: https://www.python.org/