Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Tested with a 5 line file. Credits to http://pleac.sourceforge.net/pleac_python/fileaccess.html, see section "Modifying a File in Place Without a Temporary File". See also http://stackoverflow.com/questions/125703/how-do-i-modify-a-text-file-in-python

Tested with a 5 line file. Credits to http://pleac.sourceforge.net/pleac_python/fileaccess.html, see section "Modifying a File in Place Without a Temporary File". See also https://stackoverflow.com/questions/125703/how-do-i-modify-a-text-file-in-python

Post Undeleted by Faheem Mitha
added 803 characters in body
Source Link
Faheem Mitha
  • 36.1k
  • 33
  • 129
  • 189
filename = "foo"
f = open(filename, 'r+')
s = f.read().strip()                                                                                                                                                  
s_splitlinenums = s.split("\n")                                                                                                                                        [1, 3]                                                                                                                                            
# Remove lines 1 and 3                                                                                                                                  s = [y for x, y in enumerate(f) if x not in [line-1 for 
linenumsline =in range(1,3)
forlinenums]] linenum in linenums:
    s_split.pop(linenum-1)                                                                                                                                   
f.seek(0)
f.write('\n'''.join(s_splits))
f.truncate(f.tell())
f.close()

BTW, sometimes the Python docs really suck. See http://docs.python.org/library/functions.html#openSome notes:

Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file).

Does this mean anything to you? What the hell is "open for updating"?

  1. One could first truncate the file, then write to it, rather than write, then truncate, as above. However, I don't know of a Python flag that allows one to read, and then do a truncated write. But maybe I'm missing something, as the document isn't all that clear. Which brings me to

  2. Sometimes the Python docs really suck. See http://docs.python.org/library/functions.html#open

    Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file).

    Does this mean anything to you? What the hell is "open for updating"?

  3. I don't know if doing this in python as opposed to something unixy like the stream editor is better. It might be more portable, but I don't know how portable sed is. I just wrote it like that because I'm more comfortable with low level programming than using classic unix tools, which are good if they do exactly what you want, but (I think) are generally less flexible.

  4. This approach (manipulating the file in memory) trades memory for disk space. It should work Ok on machines with a few Gb of memory for files up to a few hundred Mb. Python doesn't handle strings very efficiently, so switching to C/C++ for example would slightly increase performance and greatly reduce memory usage.

filename = "foo"
f = open(filename, 'r+')
s = f.read().strip()                                                                                                                                                  
s_split = s.split("\n")                                                                                                                                                                                                                                                                                     
# Remove lines 1 and 3                                                                                                                                                
linenums = range(1,3)
for linenum in linenums:
    s_split.pop(linenum-1)                                                                                                                                   
f.seek(0)
f.write('\n'.join(s_split))
f.truncate(f.tell())
f.close()

BTW, sometimes the Python docs really suck. See http://docs.python.org/library/functions.html#open

Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file).

Does this mean anything to you? What the hell is "open for updating"?

filename = "foo"
f = open(filename, 'r+')                                                                                                                                 
linenums = [1, 3]                                                                                                                                            
s = [y for x, y in enumerate(f) if x not in [line-1 for line in linenums]]                                                                                                                                          
f.seek(0)
f.write(''.join(s))
f.truncate(f.tell())
f.close()

Some notes:

  1. One could first truncate the file, then write to it, rather than write, then truncate, as above. However, I don't know of a Python flag that allows one to read, and then do a truncated write. But maybe I'm missing something, as the document isn't all that clear. Which brings me to

  2. Sometimes the Python docs really suck. See http://docs.python.org/library/functions.html#open

    Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file).

    Does this mean anything to you? What the hell is "open for updating"?

  3. I don't know if doing this in python as opposed to something unixy like the stream editor is better. It might be more portable, but I don't know how portable sed is. I just wrote it like that because I'm more comfortable with low level programming than using classic unix tools, which are good if they do exactly what you want, but (I think) are generally less flexible.

  4. This approach (manipulating the file in memory) trades memory for disk space. It should work Ok on machines with a few Gb of memory for files up to a few hundred Mb. Python doesn't handle strings very efficiently, so switching to C/C++ for example would slightly increase performance and greatly reduce memory usage.

Post Deleted by Faheem Mitha
Source Link
Faheem Mitha
  • 36.1k
  • 33
  • 129
  • 189

Just read it into memory, alter it, then write it back. You can do something like

filename = "foo"
f = open(filename, 'r+')
s = f.read().strip()                                                                                                                                                  
s_split = s.split("\n")                                                                                                                                                                                                                                                                                     
# Remove lines 1 and 3                                                                                                                                                
linenums = range(1,3)
for linenum in linenums:
    s_split.pop(linenum-1)                                                                                                                                   
f.seek(0)
f.write('\n'.join(s_split))
f.truncate(f.tell())
f.close()

Tested with a 5 line file. Credits to http://pleac.sourceforge.net/pleac_python/fileaccess.html, see section "Modifying a File in Place Without a Temporary File". See also http://stackoverflow.com/questions/125703/how-do-i-modify-a-text-file-in-python

BTW, sometimes the Python docs really suck. See http://docs.python.org/library/functions.html#open

Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file).

Does this mean anything to you? What the hell is "open for updating"?