0

So, I am at exercise 16 of Zed Shaw's Python book.

I thought of trying out both append and truncate on the same file. I know, this does not make sense. But, I am new and I am trying to learn what would happen if I used both.

So, first I am opening the file in append mode and then truncating it and then writing into it.

But, the truncate is not working here and whatever I write gets appended to the file.

So, can someone kindly explain why the truncate would not work ? Even though I am opening the file in append mode first, I believe I am calling the truncate function after that and it should have worked !!!

Following is my code:

from sys import argv

script, filename = argv

print "We're going to erase %r." %filename
print "If you don't want that. hit CTRL-C (^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'a')

print "Truncating the file. Goodbye!"
target.truncate()

print "Now I'm going to ask you for three lines."

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "I'm going to write these to the file."


target.write(line1 + "\n" + line2 + "\n" + line3)

print "And finally, we close it."
target.close()
Sarat
  • 77
  • 3
  • 7
  • 1
    Possible duplicate of [Behaviour of truncate() method in Python](http://stackoverflow.com/questions/26917197/behaviour-of-truncate-method-in-python) – Morgan Thrapp Jul 25 '16 at 16:23
  • But, I dint start out with an empty file. The file I am using has data in it. – Sarat Jul 25 '16 at 16:27
  • As does the dupe. It's exactly the same, they're working on the same problem from the same tutorial. – Morgan Thrapp Jul 25 '16 at 16:28
  • Try replacing the 3rd line from your script with `filename = "yourfile.txt"` and see if it still does not work. This code works properly when I run it. – Amal Rajan Jul 25 '16 at 16:31
  • I tried replacing with my file name. But, I am getting an error "NameError: name 'ex16_sample' is not defined" whereas I have the file created. – Sarat Jul 25 '16 at 16:35
  • @Sarat Sorry, but if that's your text file, you should be writing the extension `.txt` along with it. – Amal Rajan Jul 25 '16 at 16:36
  • Hi Amal, I found the solution as suggested by Pirheas below. Thanks for your help. – Sarat Jul 25 '16 at 16:52

2 Answers2

1

Truncate the file’s size. If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position.

When you open your file with 'a' mode, the position is at the end of the file.

You can do something like this

f = open('myfile', 'a')
f.tell()  # Show the position of the cursor
# As you can see, the position is at the end
f.seek(0, 0) # Put the position at the begining
f.truncate() # It works !!
f.close()
Pirheas
  • 173
  • 2
  • 7
  • I changed my code as above and it worked !!! Thank you so much. I had no idea about the cursor position and setting it to (0, 0). Appreciate your help. – Sarat Jul 25 '16 at 16:43
  • @Sarat If this solution worked for you, please consider marking it as the accepted solution. – Amal Rajan Jul 25 '16 at 17:00
  • How do I do that ? - Ok, I clicked on the green arrow - I hope, thats how it is done. – Sarat Jul 25 '16 at 17:03
0

The argument 'a' opens the file for appending. You will need to use 'w' instead.

  • Does that matter if he wants to just erase the contents of the file? – Amal Rajan Jul 25 '16 at 16:29
  • Yes. What I m a trying to understand is - if I use 'w' and not use truncate, then anyway the file will get truncted and re-written. So, what if I use an 'a' and then 'truncate' !! If truncate does not need to be specified with a 'w' and not working (in my code) with 'a' - then, what does it do anyway and how ? – Sarat Jul 25 '16 at 16:30