1、目录有中文
如直接写
f=open('Users/librom/Documents/Python/临时文件/模拟卷/天龙八部.py ','r')
就会报错
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
f=open('Users/librom/Documents/Python/临时文件/模拟卷/天龙八部.py ','r')
FileNotFoundError: [Errno 2] No such file or directory: 'Users/librom/Documents/Python/临时文件/模拟卷/天龙八部.py '
2个解决方法
- 把中文都改为英文
- 更改当前工作目录到指定的路径
import os
os.chdir(r'/Users/librom/Documents/临时文件/模拟卷')
2、当用mac终端查看文件路径时,如果其中有文件的文件名在文字之间有空格,在中端显示的时候会自动变成一个反斜杆加空格。
详见:https://blog.csdn.net/weixin_42884201/article/details/81515683
3、在window下路径一般是\,而在python的路径搜索中需要/,所以需要注意转换。另外改为\也可以。(python中\表示转义符,但\表示的是反斜杆。)
当前python版本3.7.4