标题:file类使用出错,怎么回事?
取消只看楼主
zjsxwc
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:252
专家分:601
注 册:2011-1-20
结帖率:93.75%
已结贴  问题点数:10 回复次数:0 
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 编辑 ]
搜索更多相关主题的帖子: color file 
2012-05-04 16:00



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-366941-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.806294 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved