我正在尝试了解有关 python 的更多信息并编写了一个简单的脚本,但我无法让 read() 函数工作。我错过了什么?我收到的错误消息是:
回溯(最后一次调用):文件“ex16demo.py”,第 28 行,打印 glist.read() IOError: File not open for reading
I 文件应该打开并分配给 glist 变量。
from sys import argv
script, filename = argv
print "We are creating a new grocery list!"
print "Opening %r..." % filename
glist = open(filename, 'w')
print "Deleting previous content from %r......" % filename
glist.truncate()
print "Add your items now:"
item1 = raw_input("item 1:")
item2 = raw_input("item 2:")
item3 = raw_input("item 3:")
print "Adding your items to the list...."
glist.write(item1)
glist.write("\n")
glist.write(item2)
glist.write("\n")
glist.write(item3)
glist.write("\n")
print "Here are the items in your grocery list:"
print glist.read()
谢谢!