Search code examples
pythonpython-3.xfile-handling

How to efficiently append a new line to the starting of a large file?


I want to append a new line in the starting of 2GB+ file. I tried following code but code OUT of MEMORY error.

myfile = open(tableTempFile, "r+")
myfile.read() # read everything in the file
myfile.seek(0) # rewind
myfile.write("WRITE IN THE FIRST LINE ")
myfile.close();
  1. What is the way to write in a file file without getting the entire file in memory?
  2. How to append a new line at starting of the file?

Solution

  • If you can afford having the entire file in memory at once:

    first_line_update = "WRITE IN THE FIRST LINE \n"
    with open(tableTempFile, 'r+') as f:
      lines = f.readlines()
      lines[0] = first_line_update
      f.writelines(lines)
    

    otherwise:

    from shutil import copy
    from itertools import islice, chain 
    # TODO: use a NamedTemporaryFile from the tempfile module
    first_line_update = "WRITE IN THE FIRST LINE \n"
    with open("inputfile", 'r') as infile, open("tmpfile", 'w+') as outfile:
      # replace the first line with the string provided:
      outfile.writelines(
        (line for line in chain((first_line_update,), islice(infile,1,None)))
      # if you don't want to replace the first line but to insert another line before
      # this simplifies to:
      #outfile.writelines(line for line in chain((first_line_update,), infile))
    copy("tmpfile", "infile")
    # TODO: remove temporary file