【发布时间】:2015-06-05 22:35:26
【问题描述】:
我有一个 python 脚本,它通过搜索文件夹并拉入文件列表来构建文件。当我在 IDLE 中打开并运行它时,该文件运行良好并且按预期工作,但如果我在命令行窗口中运行脚本时,我会收到此错误:
C:\Windows\system32>python "C:\Users\ntreanor\Documents\RV Scripts\Server RV Sequence.py"
Traceback (most recent call last):
File "C:\Users\ntreanor\Documents\RV Scripts\Server RV Sequence.py", line 69,
in <module>
for foldername in os.listdir(pngFolders):
WindowsError: [Error 3] The system cannot find the path specified:
'Y:/20_temp_script_testing/pr126 movs\\04_comp_pngs/*.*'
如果不明显,是的,路径确实存在。它不仅在 IDLE 中有效,而且我仔细检查过,路径确实存在。
我还尝试使用作为守护程序运行的脚本创建文件夹并得到类似的结果
Traceback (most recent call last):
File "D:\shotgun\shotgunEventDaemon.py", line 888, in process
self._callback(self._shotgun, self._logger, event, self._args)
File "D:\shotgun\plugins\CreateAssetFolders.py", line 72, in createAssetFolders
os.makedirs(folder)
File "D:\Python27\Lib\os.py", line 150, in makedirs
makedirs(head, mode)
File "D:\Python27\Lib\os.py", line 150, in makedirs
makedirs(head, mode)
File "D:\Python27\Lib\os.py", line 150, in makedirs
makedirs(head, mode)
File "D:\Python27\Lib\os.py", line 157, in makedirs
mkdir(name, mode)
WindowsError: [Error 3] The system cannot find the path specified: 'Y:/'
这是脚本在此之前作为文件夹记录的内容:
Making folder:
Y:/07_design/04_environmental_elements\eec005-08_insect_ladybird_red_7_spots_wide
(它说 Y 而不是整个路径的原因是它试图使每个文件夹返回,直到它不能再返回,这就是引发异常的时候)
命令行窗口的环境变量是否会影响应该将脚本指向正确位置的驱动器映射?
【问题讨论】:
-
你注意到你在文件名中混合了
/和\吗? -
/ 和 \\ 的混合工作正常,如果代码在 Linux 上运行的可能性很小,建议使用 /。如果您不这样做,那么在您将代码带到 *NIX 的休息日,您就会变得一团糟。是 * 给他带来了麻烦。
-
我做到了,是的。目前我正在逐字声明基本路径(因为这只是测试),然后使用 os.path.join 来创建其他文件夹,所以加入方法就是放在'\\'中。
-
我可以复制您的错误消息的唯一方法是尝试打开一个不存在的文件夹,但您说您检查了路径并以管理员身份运行。还有什么要了解的吗?
-
可能有,但我想我也不知道。我添加了一个检查目录是否存在并且它说它不在命令行上,但 IDLE 脚本仍然运行良好。我会和我们的 IT 人员谈谈,也许服务器周围还有更多我不知道的异常权限。