【发布时间】:2014-05-04 12:03:35
【问题描述】:
import os
def closed(x):
if f.closed == True:
return "closed"
else:
return "open"
f = open("test.txt", "ab")
print "Name of the file: ", f.name
print "we just opened the file so the file is: ", closed(f)
f.write("I am writing now to the file %s \n") % (f.name)
f.close()
print "now we typed f.close(), so it is: ", closed(f)
它给了我错误,并打印到 test.txt %s 并且不理解 %s?为什么?
Traceback(最近一次调用最后一次): 文件“attempt1.py”,第 12 行,在 f.write("我现在正在写入文件 %s \n") % (f.name) TypeError: 不支持的操作数类型 %: 'NoneType' 和 'str'
我希望它使用 %s 写入“我现在正在写入文件 test.txt”
【问题讨论】: