Is it possible to modify lines in a file in-place?
fileinput
module has very ugly API, I find beautiful module for this task - in_place, example for Python 3:
import in_place
with in_place.InPlace('data.txt') as file:
for line in file:
line = line.replace('test', 'testZ')
file.write(line)
main difference from fileinput:
- Instead of hijacking
sys.stdout
, a new filehandle is returned for writing. - The filehandle supports all of the standard I/O methods, not just
readline()
.
Important Notes:
- This solution deletes every line in the file if you don't re-write it with the
file.write()
line. - Also, if the process is interrupted, you lose any line in the file that has not already been re-written.
Is it possible to parse a file line by line, and edit a line in-place while going through the lines?
It can be simulated using a backup file as stdlib's fileinput
module does.
Here's an example script that removes lines that do not satisfy some_condition
from files given on the command line or stdin
:
#!/usr/bin/env python
# grep_some_condition.py
import fileinput
for line in fileinput.input(inplace=True, backup='.bak'):
if some_condition(line):
print line, # this goes to the current file
Example:
$ python grep_some_condition.py first_file.txt second_file.txt
On completion first_file.txt
and second_file.txt
files will contain only lines that satisfy some_condition()
predicate.