19

This is from exercise 16 from Zed Shaw's Python tutorials. I'm having a hard time understanding what exactly the truncate function does in this case. So the logic is that we open a file and then...shorten it? For what? What exactly is happening here?

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, 'w')

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 + "\n")

print "And finally, we close it."
target.close()
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
J82
  • 8,267
  • 23
  • 58
  • 87

2 Answers2

28

You're right to be suspicious.

First, file.truncate does this:

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…

Not quite the same as Zed's description—it only "empties the file" if the current position is the start of the file—but since we just opened the file (and not in a mode), the current position is the start, so that isn't relevant. We're truncating to an empty file.

Which is all well and good, except that open already does that:

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists) …

So, we open the file, creating it if it doesn't exist and truncating it to 0 bytes if it does. Then, on the next line, we truncate it to 0 bytes.

(That "Truncating the file. Goodbye!" message is pretty misleading, since we've already truncated it. Imagine you put a breakpoint on that line and decided to kill the program before executing it…)

But notice that this isn't some silly mistake by Zed; he appears to have done this specifically to make the point in study drill #5:

If you open the file with 'w' mode, then do you really need the target.truncate()? Read the documentation for Python's open function and see if that's true.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 1
    On top of being misleading, this also slightly reduces the portability of the program for no good reason, because some older *nix and some non-Windows/non-*nix platforms don't have `truncate` in Python 2.x… – abarnert Nov 13 '14 at 19:58
1

I am currently going through Zed Shaw's book to. Often. when he gives you problems like these, he is goading you into messing around with the different elements of your code to see what they do. Feel free to delete things like target.truncate() and re-run the program. As abarnert says, there is a way to make truncate only remove a portion of the file. Leaving this hole in your knowledge, as to how to get different results with these two options, is meant to annoy you into doing some independent research. It's a very effective way of infecting the reader with curiosity. Take a look how append works in Python and see if you can make truncate() remove only the last line of your test file. Don't be afraid to break your code. "If you want to increase your success rate, double your failure rate." (Thomas J. Watson, former CEO of IBM)