开发者

How to insert a new line before the first line in a file using python?

开发者 https://www.devze.com 2023-02-16 17:26 出处:网络
More details below: 1st line 2nd line 3rd line 4th line ... Now want to insert a new line named zero line before 1st line. File looks like below:

More details below:

1st line

2nd line

3rd line

4th line

...

Now want to insert a new line named zero line before 1st line. File looks like below:

开发者_StackOverflow
zero line

1st line

2nd line

3rd line

4th line

...

I know sed command can do this work, but how to do it using python? Thanks


you can use fileinput

>>> import fileinput
>>> for linenum,line in enumerate( fileinput.FileInput("file",inplace=1) ):
...   if linenum==0 :
...     print "new line"
...     print line.rstrip()
...   else:
...     print line.rstrip()
...


this might be of interest

http://net4geeks.com/index.php?option=com_content&task=view&id=53&Itemid=11

adapted to your question:

# read the current contents of the file
f = open('filename')
text = f.read()
f.close()
# open the file again for writing
f = open('filename', 'w')
f.write("zero line\n\n")
# write the original contents
f.write(text)
f.close()
  • Open the file and read the contents into 'text'.

  • Close the file

  • Reopen the file with argument 'w' to write

  • Write text to prepend to the file

  • Write the original contents of the file to the file

  • Close file

Read the warnings in the link.

edit:

But note that this isn't entirely safe, if your Python session crashes after opening the file the second time and before closing it again, you will lose data.


Here's an implementation that fixes some deficiencies in other approaches presented sofar:

  • it doesn't lose data in case of an error — @kriegar's version does
  • supports empty files — fileinput version does not
  • preserves original data: doesn't mangle trailing whitespace — fileinput version does
  • and does not read the whole file in memory as the version from net4geeks.com does.

It mimics fileinput's error handling:

import os

def prepend(filename, data, bufsize=1<<15):
    # backup the file
    backupname = filename + os.extsep+'bak'
    try: os.unlink(backupname) # remove previous backup if it exists
    except OSError: pass
    os.rename(filename, backupname)

    # open input/output files,  note: outputfile's permissions lost
    with open(backupname) as inputfile, open(filename, 'w') as outputfile:
        # prepend
        outputfile.write(data)
        # copy the rest
        buf = inputfile.read(bufsize)
        while buf:
            outputfile.write(buf)
            buf = inputfile.read(bufsize)

    # remove backup on success
    try: os.unlink(backupname)
    except OSError: pass

prepend('file', '0 line\n')

You could use cat utility if it is available to copy the files. It might be more efficient:

import os
from subprocess import PIPE, Popen

def prepend_cat(filename, data, bufsize=1<<15):
    # backup the file
    backupname = filename + os.extsep+'bak'
    try: os.unlink(backupname)
    except OSError: pass
    os.rename(filename, backupname)

    # $ echo $data | cat - $backupname > $filename
    with open(filename, 'w') as outputfile: #note: outputfile's permissions lost
        p = Popen(['cat', '-', backupname], stdin=PIPE, stdout=outputfile)
        p.communicate(data)

    # remove backup on success
    if p.poll() == 0:
        try: os.unlink(backupname)
        except OSError: pass

prepend_cat('file', '0 line\n')


with open(filename, 'r+') as f:
    lines = f.readlines()     
    lines.insert(0, 'zero line\n')   
    f.seek(0)                 
    f.writelines(lines)  


code

L = list()
f = open('text2.txt', 'r')
for line in f.readlines():
        L.append(line)
L.insert(0,"Zero\n")
f.close()

fi = open('text2.txt', 'w')
for line in xrange(len(L)):
        fi.write(L[line])

fi.close()

text2.txt

Hello
The second line
3
4
5
6

output

Zero
Hello
The second line
3
4
5
6

This can be memory heavy and time consuming for large files however.

If you are worried about something like 31st line, I would just do a mod%10 on the num, to get a more accurate version.

Let me know if this helps, or if you want a better version. Also, if you want a better formatting, look into ljust and rjust for left and right justify.

0

精彩评论

暂无评论...
验证码 换一张
取 消