[Tutor] Closing triple quotation marks.

Lisi lisi.reisz at gmail.com
Sat Jun 18 13:52:13 CEST 2011


I have left the program intact below* because I am afraid that if I cut it I 
may cut out something that is essential.  I am working from an existing 
program that I have typed out under instructions, but am now trying to 
replace the commented out section with just one command.  The program with 
the commented out section worked fine.

I have been stuck for some hourslast night andagain this morning.  I have 
inserted the print command after the commented out section in order simply to 
try out whether the triple quotation marks ever work as I thought they did, 
and the answer is yes they do.

If I leave the target.write without the final """, I get the error (which I 
would indeed expect):

lisi at Tux:~/Python/LearnPythonTheHardWay$ python extra-credit_16a.py 
learning.txt
  File "extra-credit_16a.py", line 42

    ^
SyntaxError: EOF while scanning triple-quoted string
lisi at Tux:~/Python/LearnPythonTheHardWay

Fair enough. the closing quotation marks are not there.

But when they _are_ there, i.e. when that stanza reads:

target.write """
line1\nline2\nline3\n
"""

I get:

lisi at Tux:~/Python/LearnPythonTheHardWay$ python extra-credit_16a.py 
learning.txt
  File "extra-credit_16a.py", line 38
    """
      ^
SyntaxError: invalid syntax

So where should I be putting those wretched triple quotes?  I have been stuck 
on this for hours.

With the target.write stanza there, none of the rest of the program works 
either.  If I comment out the target.write stanza and leave the print stanza 
above it alone, the whole program runs as I would expect it to.

I have searched the Python.org site; I have googled; I am stuck and have run 
out of ideas.

Lisi

*Herewith the program:

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("line1: ")
line2=raw_input("line2: ")
line3=raw_input("line3: ")

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

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

print """
line1\nline2\nline3\n
"""

target.write """
line1\nline2\nline3\n

print "And finally we close it."
target.close()


More information about the Tutor mailing list