Windows 10中命令提示符下绝对文件路径的Python打开错误

2024-03-29 13:35:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有Windows10上Python3.6的Anaconda3发行版。我正在从命令窗口对将创建并写入文本文件的脚本进行以下调用:

C:\WINDOWS\system32>python C:\Users\shawn\Documents\Google_Maps_Drive_Times\Commute_Times.py

并得到以下错误:

Traceback (most recent call last):
  File "C:\Users\shawn\Documents\Google_Maps_Drive_Times\Commute_Times.py", line 97, in <module>
    with open(ff, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\WINDOWS\\system32\\Daily_Trip_Times\\Commute_Times_05-26-18_1558.txt'

奇怪的是,当我使用Spyder IDE运行它时,代码运行得非常好。更奇怪的是,如果我添加

ff = ff.replace('\\','\')

然而,正如我所料,这会破坏Spyder的功能。我也尝试过使用反斜杠,但是得到了与“\\”相同的错误。你知道吗

注意,我已经编写了一些简单的脚本来验证Python是否正常工作和

where python

提供

C:\ProgramData\Anaconda3\python.exe

如我所料。在理解Spyder和命令提示符之间的路径目录细微差别方面的任何帮助都将不胜感激。你知道吗


Tags: py脚本windowsgoogledriveusersdocumentsmaps