我是python的业余爱好者,我相信这有一个简单的解决方案。我正在尝试调用一个使用os.path.exists函数的模块。如果我写下面的内容,代码运行正常。
import os
if os.path.exists('text.txt'):
print 'yes'
但是,我收到此错误:“NameError:全局名称'os'未定义”当我调用模块时如下 -
import os
import modutest
modutest.test()
使用此模块,(我称之为modutest.py)
def test():
if os.path.exists('text.txt'):
print 'yes'
答案 0 :(得分:5)
只需在modutest.py中导入'os'模块,如下所示:
import os
def test():
if os.path.exists('text.txt'):
print 'yes'
modutest.py是完全不同的文件,因此您必须导入'os'模块。