无法了解此错误
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
这是我的文件内容
from sys import argv
from os.path import exists
script,source,desti=argv
src_file=open(source)
data=src_file.read()
print "does the file exists %r" % exists(src_file)
dest_file=open(desti,'r+')
dest_file.write(data)
print dest_file.read()
该os.path.exists()
函数采用一个字符串,其中包含一个文件名。您传入了一个打开的文件对象(open(source)
调用的结果)。
您可以改用source
包含文件名的文件:
print "does the file exists %r" % exists(source)
由于您已经成功从文件名打开了文件,因此测试文件是否存在没有意义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句