1

Why does Python-2.7 on Windows truncate a file? The problem is well known with shutil.copyfile but I don't understand how to avoid it when I'm changing the first line. File size is unknown and could be huge.

Reference below but I'd prefer better exception handling with something like the following code:

import os
import sys
import shutil

with open(sys.argv[1], 'r+') as src:
    line = src.readline()
    with open(sys.argv[1], 'r+') as dst:
        dst.write = sys.argv[1]+'\n'
        shutil.copyfileobj(src, dst)

Reference: change first line of a file in python

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
flywire
  • 1,155
  • 1
  • 14
  • 38
  • 2
    Opening a file for writing _always_ truncates it, no matter Windows, Linux, OSX, Python or whatever. In your case, the only option is to create a new file, write the first new line into it, and then copy the original file to the end of the new file (the files must have different names). – DYZ Feb 11 '18 at 08:31
  • You have to open with a mode of something like `w+`. Make sure the new first line is *exactly* the same length as the original (if its shorter then you will get some of the old line left, if it is longer then you will overwrite part of the second line). – cdarke Feb 11 '18 at 09:02

1 Answers1

4

You need to create the new version of the file as a NamedTemporaryFile. After you finish constructing it, you then rename it on top of the old file.

Code:

def insert_line_front(insert_filename, to_insert):

    with open(insert_filename) as src, tempfile.NamedTemporaryFile(
            'w', dir=os.path.dirname(insert_filename), delete=False) as dst:

        # Discard first line
        src.readline()

        # Save the new first line
        dst.write(to_insert + '\n')

        # Copy the rest of the file
        shutil.copyfileobj(src, dst)

    # remove old version
    os.unlink(insert_filename)

    # rename new version
    os.rename(dst.name, insert_filename)

    return()

Test Code:

import os
import shutil
import sys
import tempfile

# For noob - Function code goes here

filename = os.path.abspath(sys.argv[1])
insert_line_front(filename, filename)

Before:

/testcode/file1
"-3.588920831680E-02","1.601887196302E-01","1.302309112549E+02"
"3.739478886127E-01","1.782759875059E-01","6.490543365479E+01"
"3.298096954823E-01","6.939357519150E-02","2.112392578125E+02"
"-2.319437451661E-02","1.149862855673E-01","2.712340698242E+0

After:

/testcode/file2
"-3.588920831680E-02","1.601887196302E-01","1.302309112549E+02"
"3.739478886127E-01","1.782759875059E-01","6.490543365479E+01"
"3.298096954823E-01","6.939357519150E-02","2.112392578125E+02"
"-2.319437451661E-02","1.149862855673E-01","2.712340698242E+0
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135