try:
print(filePath)
my_file = Path(filePath)
except ValueError:
print ValueError
尝试运行此代码会不断抛出错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 26: ordinal not in range(128)
炸掉的值是: Kellyanne Conway: Giuliani’s Withdrawal From Consideration A ‘Mutual Decision’ | MTP Daily | MSNBC.opus
我试过用ascii和unicode编码此字符串,但是它没有用。谢谢,我如何检查文件是否正确存在。
使用os.path而不是pathlib。
import os
print os.path.exists(filePath)
print os.path.isfile(filePath)
Python 3之前的pathlib不能很好地处理非ASCII字符。看:
为什么Python2和Python3对同一个Windows目录有不同的对待?
和
https://pypi.python.org/pypi/pathlib/(搜索“ ASCII”)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句