I'm working through Learn Python the Hard Way, and trying to understand it rather than just hammer away. I got stuck on Exercise 16, as discussed already on SO here:
Very basic Python question (strings, formats and escapes)
but I'm still trying to figure out why this approach does not work:
from sys import argv
script, filename = argv
print "Attempting to open the file now."
print open(filename).read()
print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-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."
linebreak = "\n"
target.write("%s %s %s %s %s %s") % (line1, linebreak, line2, linebreak, line3, linebreak)
target.write("the ending line")
print "And finally, we close it."
target.close()
I've established a value for linebreak, and am calling the line1, line2 and linebreak values with %s in the target.write command. Should't it parse as "line1 \n line2 \n line3 \n" when it's read?
This is probably the equivalent of being asked by a child what keeps the sky up or something, and I apologize for being kind of thick. Thanks!