2

In regards to the related question, I'd like to know how to add text and/or lines to the beginning of a file in Python, as its been suggested that is an easier language for text/file manipulation. So, while I asked the previous, linked, question for C++, can anyone point me to how to do this with Python?

Quote from the linked (related) question:

I'd like to be able to add lines to the beginning of a file.

This program I am writing will take information from a user, and prep it to write to a file. That file, then, will be a diff that was already generated, and what is being added to the beginning is descriptors and tags that make it compatible with Debian's DEP3 Patch tagging system.

Anyone got any suggestions, or code?


Related: Adding text and lines to the beginning of a file (C++)

Community
  • 1
  • 1
Thomas Ward
  • 2,714
  • 7
  • 36
  • 51
  • 6
    See http://stackoverflow.com/questions/4454298/prepend-a-line-to-an-existing-file-in-python – Danilo Bargen Jun 27 '12 at 15:31
  • I would prefer to see that some effort has been made in searching through stackoverflow for similar questions asked earlier and why the answers there does not satisfy your question. We often see repeated questions. It is not a good for SO or for community as we don't move forward. – pyfunc Jun 27 '12 at 16:09
  • @pyfunc, from this system here, the search function times out. There is no other system I can use for searching. I've marked this, my own question, with a close vote as Exact Dupe for the link already posted here in comments, anyone is welcome to do that as well. (system won't let me delete) – Thomas Ward Jun 27 '12 at 16:25
  • Ah! mention it next time. Sorry about that. – pyfunc Jun 27 '12 at 16:27

2 Answers2

7

There are lot of similar file I/O questions recently..

In short, you need to create a new file

  1. first, write new lines to the file
  2. read lines from old file and write them to file

If you can guarantee each of your new lines added to the beginning is longer than the each of the corresponding original lines at the beginning, you can do it in place:

f = open('file.txt','r+')
lines = f.readlines() # read old content
f.seek(0) # go back to the beginning of the file
f.write(new_content) # write new content at the beginning
for line in lines: # write old content after new
    f.write(line)
f.close()

The example above writes all the data in it's entirety after seeking it's position at beginning of file because the contents of file are over-written with new contents.

Otherwise you need to write to a new file

f = open('file.txt','r')
newf = open('newfile.txt','w')
lines = f.readlines() # read old content
newf.write(new_content) # write new content at the beginning
for line in lines: # write old content after new
    newf.write(line)
newf.close()
f.close()
xvatar
  • 3,229
  • 17
  • 20
1

Something like this should work:

with open('new.txt', 'w') as new:
    with open('prefix.txt') as prefix:
        new.write(prefix.read())
    with open('old.txt') as old:
        new.write(old.read())

If old.txt or prefix.txt contain binary contents, you should add an 'rb' argument to their respective open calls and a 'b' to the flags argument for the first open() call.

djc
  • 11,603
  • 5
  • 41
  • 54