Python 当 运行 来自 IDLE 但不是命令行时,脚本可以看到 Y:/ 驱动器

Python Script can see the Y:/ Drive when run from IDLE but not the command line

我有一个 python 脚本,它通过搜索文件夹并拉入文件列表来构建文件。这个文件 运行 很好,当我打开并 运行 它处于空闲状态时,它可以按预期工作,但是如果当我 运行 命令行中的脚本 window 我得到这个错误:

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 而不是整个路径的原因是它试图让每个文件夹返回,直到它不能再返回,这就是抛出异常的时候)

命令行的环境变量 window 是否以某种方式影响应该将脚本指向正确位置的驱动器映射?

我认为您的问题是您试图打开 * 文件,该文件当然不存在。 open(path) 将 path 作为文字字符串并且不会以任何方式对其进行翻译,因此它期望该值是有效的文件名。您应该更改代码以获取目录而不是文件,然后遍历该目录。

这个问题可能是因为 IDLE 和您的命令行 运行 不同级别的权限。映射的网络驱动器不会自动对所有用户上下文可用。这个 here and plenty of other 资源中有一个超级用户问题涵盖了这个主题。简而言之,映射的网络驱动器仅可用于映射所在级别的程序 运行。

如果您通过 windows UI 映射了网络驱动器,那么它将被映射到未提升的程序。但是,如果它是用 net use 映射的,那么它取决于进行映射时命令提示符的级别!

禁用 UAC 也会影响更改此行为,使用提升的(或不提升的)命令提示符也会影响,这可以解释为什么某些 PC 显示不同的行为。