file类使用出错,怎么回事?
程序代码:
def TT(): poem = '''\ Programming is fun When the work is done if you wanna make your work also fun: use Python! ''' f = file('poem.txt', 'w') # open for 'w'riting f.write(poem) # write text to file f.close() # close the file f = file('poem.txt') # if no mode is specified, 'r'ead mode is assumed by default while True: line = f.readline() if len(line) == 0: # Zero length indicates EOF break print (line), # Notice comma to avoid automatic newline added by Python f.close() # close the file出错如下:
程序代码:
>>> TT() Traceback (most recent call last): File "<pyshell#30>", line 1, in <module> TT() File "<pyshell#29>", line 9, in TT f = file('poem.txt', 'w') # open for 'w'riting NameError: global name 'file' is not defined
今天学了下python,定义了个TT函数,为什么一运行TT()就出现file未定义这个错误?,难道file类还要import?那么要import什么?
[ 本帖最后由 zjsxwc 于 2012-5-4 16:02 编辑 ]