2

I am a Python beginner and my next project is a program in which you enter the details of your program and then select the file (I'm using Tkinter), and then the program will format the details and write them to the start of the file.

I know that you'd have to 'rewrite' it and that a tmp file is probably in hand. I just want to know simple ways that one could achieve adding text to the beginning of a file.

Thanks.

3 Answers3

0

If I understand what you're asking, I believe you're looking for what's called a project skeleton. This link handles it pretty well.

purpleladydragons
  • 1,305
  • 3
  • 12
  • 28
  • not quite what I wanted. I probably wrote too much stuff at the start. I just want to add text to the start of a python file. But thank you for the project skeleton, I think I will use it later on. – burningcheerio Jan 12 '12 at 05:13
0

To add text to the beginning of a file, you can (1) open the file for reading, (2) read the file, (3) open the file for writing and overwrite it with (your text + the original file text).

formatted_text_to_add = 'Sample text'
with open('userfile', 'rb') as filename:
    filetext = filename.read()

newfiletext = formatted_text_to_add + '/n' + filetext
with open('userfile', 'wb') as filename:   
    filename.write(newfiletext)

This requires two I/O operations and I'm tempted to look for a way to do it in one pass. However, prior answers to similar questions suggest that trying to write to the beginning or middle of a file in Python gets complicated quite quickly unless you bite the bullet and overwrite the original file with the new text.

Community
  • 1
  • 1
Ari
  • 460
  • 6
  • 13
  • Thanks! I'll have to look into that with detail, it looks a lot simpler than the other examples I've seen :D – burningcheerio Jan 12 '12 at 20:10
  • Glad this helps. Try reading and writing to files from the command line to see how it works in action. – Ari Jan 12 '12 at 20:22
  • by the way 'newfiletext = formatted_text_to_add + '/n' + filetext' throws a TypeError, and I changed the + filetext to + str(filetext), is this the best way to do it? – burningcheerio Jan 12 '12 at 20:30
  • Actually sorry I'm wrong, because when I write newfiletext it says 'str' does not support the buffer interface – burningcheerio Jan 12 '12 at 20:31
  • Not sure what's happening. My guess is that there's something wrong with formatted_text_to_add. Make sure it's a string. You can test each of the variables with the `type()` function. Also, in your code, you can use shorter variable names than I did, to make transcription errors less likely. – Ari Jan 13 '12 at 04:04
0

This probably won't solve your exact problem, as you will need to know in advance the exact number of bytes you'll be adding to the beginning of the file.

# Put some text in the file
f = open("tmp.txt", "w")
print >>f, "123456789"
f.close()

# Open the file in read/write mode
f = open("tmp.txt", "r+")
f.seek(0)             # reposition the file pointer to the beginning of the file
f.write('abc')        # use write to avoid writing new lines
f.close()

When you reposition the file pointer using seek, you can overwrite the bytes that are already stored at that position. You can't, however, "insert" text, pushing existing bytes ahead to make room for new data. When I said you would need to know the exact number of bytes, I meant you would have to "leave room" for the text at the beginning of the file. Something like:

f = open("tmp.txt", "w")
f.write("\0\0\0456789")
f.close()
# Some time later...
f = open("tmp.txt", "r+")
f.seek(0)
f.write('123')
f.close()

For text files, this can work if you leave a "blank" line of, say, 50 spaces at the beginning of the file. Later, you can go back and overwrite up to 50 bytes (the newline being byte 51) without overwriting following lines. Of course, you can leave multiple lines at the beginning. The point is that you can't grow or shrink your reserved block of lines to be overwritten. There's nothing special about the newline in a file, other than that it is treated specially by file methods like read and readline for splitting blocks of data into separate strings.

To add one of more lines of text to the beginning of a file, without overwriting what's already present, you'll have to use the "read the old file, write to a new file" solution outlined in other answers.

chepner
  • 497,756
  • 71
  • 530
  • 681