在python中传递给Exists函数的参数
无法理解此错误在python中传递给Exists函数的参数,python,python-2.7,Python,Python 2.7,无法理解此错误 Traceback (most recent call last): File "ex4.py", line 6, in <module> print "does the file exsts %r" % exists(src_file) File "C:\Python27\lib\genericpath.py", line 26, in exists os.stat(path) TypeError: coercing to Unico
Traceback (most recent call last):
File "ex4.py", line 6, in <module>
print "does the file exsts %r" % exists(src_file)
File "C:\Python27\lib\genericpath.py", line 26, in exists
os.stat(path)
TypeError: coercing to Unicode: need string or buffer, file found
os.path.exists()。您传入了一个open file对象(调用open(source)
的结果)
您可以使用source
,其中包含您的文件名:
print "does the file exists %r" % exists(source)
由于您已经成功地从文件名打开了该文件,因此测试该文件是否存在几乎没有意义
print "does the file exists %r" % exists(source)