How to Change First Line of File in Python?

2019-07-25 12:24发布

问题:

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

回答1:

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