Replacing strings in files is a common task in Python. Whether you need to update configuration files, do find-and-replace operations on text, or modify source code, knowing how to replace strings efficiently is key.
In this comprehensive guide, we‘ll cover several methods for replacing strings in files in Python:
1. Read File Contents, Modify Strings, Write Back
This method involves:
- Opening the file and reading its contents into a string variable
- Using string replacement methods like str.replace() to modify the string
- Writing the modified string back into the file (overwriting the original contents)
Here is sample code:
with open(‘file.txt‘, ‘r‘) as file:
filedata = file.read()
filedata = filedata.replace(‘old string‘, ‘new string‘)
with open(‘file.txt‘, ‘w‘) as file:
file.write(filedata)
This loads the entire file contents into memory, replaces all occurrences of ‘old string‘ with ‘new string‘, then writes the modified contents back into the file.
2. Read Line By Line, Write Modified Lines
For large files, the previous method may consume too much memory. A more memory-efficient approach is to process the file line-by-line:
with open(‘file.txt‘, ‘r‘) as read_file:
with open(‘new_file.txt‘, ‘w‘) as write_file:
for line in read_file:
line = line.replace(‘old string‘, ‘new string‘)
write_file.write(line)
This iterates through each line, replaces old string, writes the modified line into a new file. No need to load full file contents into memory.
3. Use Regular Expressions
For advanced find-and-replace, regular expressions can be used instead of plain strings:
import re
with open(‘file.txt‘, ‘r‘) as read_file:
with open(‘new_file.txt‘, ‘w‘) as write_file:
for line in read_file:
line = re.sub(r‘some pattern‘, ‘new string‘, line)
write_file.write(line)
This will replace text matching the regex ‘some pattern‘ instead of plain strings. Very powerful for complex search/replace jobs.
4. Replace In-Place Using Fileinput
The fileinput module allows modifying files in-place instead of writing a separate output file:
import fileinput
for line in fileinput.input(‘file.txt‘, inplace=True):
line = line.replace(‘foo‘, ‘bar‘)
print(line, end=‘‘)
This loads file.txt, replaces ‘foo‘ with ‘bar‘ in each line, then prints back the modified line into file.txt. Very useful for in-place batch modifications.
Conclusion
As you can see, Python provides great flexibility for replacing text in files. Whether you need simple search/replace or complex regex substitutions, file contents can be updated easily.
The choice depends on your specific needs – memory usage vs code complexity, new output file vs in-place update, etc. With all these tools at your disposal, string replacement should be a breeze!


