1

I have a little code at the bottom of my program that is supposed to add a text header to the beginning of my text file. The data writes to the bottom of the file. I was reading in 'Learn Python The Hard Way' that .seek(0) would put the insertion point at the beginning, which is at the location of 0 bytes. However, I must be implementing the function incorrectly.

print 'Now, I will write a header to the original file to display copyright.'

append_copy = open(filename, "a+")
append_copy.seek(0)
append_copy.write("Copyright -- Ryan -- 2014\n")
append_copy.close()

print 'File closed and data written!'

2 Answers 2

2

The other answer was absolutely correct that this cannot be done directly, but you do not need to create a temporary file to do what you want.

You can clobber files with Python, if you open them with the flag w, so you do not need to create a temporary file, assuming your original file was small enough to hold in memory.

filename = "Foo.txt";
print 'Now, I will write a header to the original file to display copyright.'

append_copy = open(filename, "r")
original_text = append_copy.read()
append_copy.close()

append_copy = open(filename, "w")
append_copy.write("Copyright -- Ryan -- 2014\n")
append_copy.write(original_text)
append_copy.close()

print 'File closed and data written!'

The clobbering is documented here.

'w' for only writing (an existing file with the same name will be erased)

Sign up to request clarification or add additional context in comments.

2 Comments

This way actually totally makes sense. When opening up in "w" mode, the previous data is overwritten, correct?
@Simplex, Yes, it will destroy the previous file. More generally, this is basically what every text editor has to do: Load the file into memory, let you edit it, and then when you hit save, overwrite the original file.
2

What you're doing won't work, but what you want can't be done.

Opening a file in append mode means that writes are always appended at the end of the file. That's what "append" means, so that should not entirely be a surprise.

But no filesystem allows inserting bytes at an arbitrary point regardless. You will need to create a new file, write the desired bytes to it, copy the contents of the old file, delete or rename the old file, and then rename the new file.

3 Comments

So the most efficient way would to store the string data of existing file into a variable, writing the copyright text to the file, then appending the string data from previous file into the new copy file?
No. The most efficient way would be using something like copyfileobj() after writing the bytes to the new file.
@IgnacioVazquez-Abrams: So just to make sure I understand correctly, so that I don't post a new question. The ONLY WAY to append a header line at the beginning of an already populated-by-text .txt file is by means of going through the already-populated-by-text file and saving its content, to then write that content in a new .txt file, file which has been previously populated with the header you want (a simple operation, just write a header to an empty .txt)? I have to do ''addition of a simple header'' in a .txt file with 100,000 lines and 6 columns. I cannot do it before it's populated.thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.