python文件操作的坑( FileNotFoundError: [Errno 2] No such file or directory...)

环境:Windows8.1, Python3.6  pycharm community 2017

 
c盘下有一个配置文件:setup
 
1 with open('c:\setup','r') as f:
2         f.read()

提示一直很诡异:FileNotFoundError: [Errno 2] No such file or directory: 'c:\setup'

网上有大量关于Python编码错误导致无法打开文件的解决方法,utf8 gbk2312,尝试过一遍全部无效

最后原因很乌龙:setup后缀是*.log

换成

1 filename = 'c:\setup.log' 

所有问题解决

PS:所有Windows下的文件路径,一律用双反斜杠

原文地址:https://www.cnblogs.com/fanyuchen/p/7073629.html