#import information from sys module called argv
from sys import argv
#create script and filename objects that are set by 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("%r\n,%r\n,%r\n" % (line1, line2, line3)
#target.write(line1)
#target.write("\n")
#target.write(line2)
#target.write("\n")
#target.write(line3)
#target.write("\n")
#print "And finally, we close it."
#target.close()
我正在尝试编写与我注释过的脚本类似的脚本,该脚本使用read和argv读取我刚刚创建的文件。此文件中的重复太多,因此使用字符串,格式和转义符,我想仅使用一个target.write()命令而不是六个命令来打印出line1,line2和line3,但是我一直都遇到无效的语法错误。为什么会这样?
答案 0 :(得分:1)
您缺少一个右括号。
target.write("%r\n,%r\n,%r\n" % (line1, line2, line3)
应该是
target.write("%r\n,%r\n,%r\n" % (line1, line2, line3))