【问题标题】:os.listdir raises OSError, but path exists and is corectly passed to pythonos.listdir 引发 OSError,但路径存在并正确传递给 python
【发布时间】:2018-09-27 17:07:55
【问题描述】:

我在一台用于自动测试的特定 OSX 机器上遇到问题,即 os.listdir 引发 OSError。

有趣的是:

os.path.exists("/Volumes/label_of_mounted_drive") -> True
os.path.isdir("/Volumes/label_of_mounted_drive") -> True

但是

os.listdir("/Volumes/label_of_mounted_drive") -> OSError ([Errno 2] No such file or directory)

路径的统计数据是

posix.stat_result(st_mode=16877, st_ino=1, st_dev=939524119, st_nlink=2, st_uid=502, st_gid=20, st_size=68, st_atime=1523980718, st_mtime=1523980718, st_ctime=1523980718)

驱动器已安装、可见并可在 finder 中使用。我可以通过终端列出它。我什至可以通过单独的 python 进程列出它(例如调用 subprocess 和 'python'、'-c'、'import os; print os.listdir("the_path_here")')

更有趣的是,它在 2 天前还在工作,现在停止了*。

此外 - 在终端中运行 python - listdir 在这个特定路径上工作,通过nosetests / unittests 运行测试 - listdir 已损坏。

现在这不是这台特定机器上第一次发生这种情况。上次我只是重新安装了整个系统并修复了它。 现在 - 我也打算这样做 - 但此时 我非常好奇是什么原因造成的。

有什么想法吗?

*在我从 virtualenv 切换到 pyenv-virtualenv 后它停止工作。但是经过一段时间和许多其他尝试后,我卸载了所有 brew 包和 brew 本身,创建了新用户,删除了旧用户,从头开始在新用户上安装了 brew 和 python,然后回到简单的 virtualenv,一切都没有成功: (

机器设置是:

  • 塞拉利昂 10.12.6
  • 酿造
  • python@2
  • xcode 9.2
  • 一些已安装的库是:
    • 酿造:zlib、libjpeg
    • python:matplotlib、Pillow、lxml、pyobjc

【问题讨论】:

    标签: python macos matplotlib operating-system listdir


    【解决方案1】:

    我已经对此进行了一些研究,看起来这些是可以尝试的:

    1. 如果你想扩展/Volumes/label_of_mounted_drive,那么我相信你应该做os.listdir(os.path.expanduser('/Volumes/label_of_mounted_drive'))
    2. 如果您还没有,请使用完整路径。 (用户/您的用户名/随便)
    3. 如果一切都失败了,请尝试os.chdir()

    我希望这会有所帮助。

    【讨论】:

    • /Volumes/ 就像您插入笔式驱动器或将其安装在 /Volumes 下的东西,或者如果您有多个驱动器(分区等)都列在 /Volumes 下。 /Volumes 不在用户空间下,但用户空间在“内部”/Volumes(如 /Volumes/Macintosh HD/Users),因此相对于用户空间制作 /Volumes 没有意义。我会试试 os.chdir。
    【解决方案2】:

    事实证明,导入 matplotlib 会破坏这一点(测试在 virtualenv 上运行)。为什么-我不知道。 我将绘图移至运行测试后,并在方法中导入 matplotlib。

    【讨论】:

    • 这可能不是真正的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 2020-03-14
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多