【问题标题】:subprocess python filenotfounderror: [winerror 2]子进程 python filenotfounderror: [winerror 2]
【发布时间】:2017-08-22 15:19:47
【问题描述】:

我一直在使用 Jupyter Notebook 学习Principal Component Analysis from kaggle),但是当我运行这段代码时

     from subprocess import check_output
     print(check_output(["ls", "../input"]).decode("utf8"))

下面出现错误

FileNotFoundError Traceback (most recent call last)
<ipython-input-3-de0e39ca3ab8> in <module>()
      1 from subprocess import check_output
----> 2 print(check_output(["ls", "C:/Users/wanglei/Documents/input"]).decode("utf8"))

D:\Anaconda3\lib\subprocess.py in check_output(timeout, *popenargs, **kwargs)
    624 
    625     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
--> 626                **kwargs).stdout
    627 
    628 

D:\Anaconda3\lib\subprocess.py in run(input, timeout, check, *popenargs, **kwargs)
    691         kwargs['stdin'] = PIPE
    692 
--> 693     with Popen(*popenargs, **kwargs) as process:
    694         try:
    695             stdout, stderr = process.communicate(input, timeout=timeout)

D:\Anaconda3\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds)
    945                                 c2pread, c2pwrite,
    946                                 errread, errwrite,
--> 947                                 restore_signals, start_new_session)
    948         except:
    949             # Cleanup if the child failed starting.

D:\Anaconda3\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
   1222                                          env,
   1223                                          cwd,
-> 1224                                          startupinfo)
   1225             finally:
   1226                 # Child is launched. Close the parent's copy of those pipe

路径是正确的,似乎所有来自子进程的调用都会同样失败。

有人知道为什么会这样吗?

【问题讨论】:

    标签: python python-3.x subprocess jupyter-notebook kaggle


    【解决方案1】:

    尝试通过添加 cwd 参数来指定工作目录,这应该可以工作

    import os, subprocess
    cwd = os.getcwd()
    proc = subprocess.Popen(["ls" ,"../input"], cwd=cwd, stdout=subprocess.PIPE)
    output = proc.stdout.read().decode("utf8")
    

    【讨论】:

    • 这已经是cwd 的默认值。这里的问题也与ls的参数无关(如果是,则不会出现FileNotFound错误,而是来自ls的错误消息),而是二进制文件本身。
    • 不幸的是,它抛出了同样的错误。这可能是子进程模块的问题吗?
    【解决方案2】:

    此代码运行ls command,其中is available on all POSIX-conforming systems

    您使用的是 Microsoft Windows。默认情况下,Microsoft Windows 不符合 POSIX。例如,没有ls 二进制文件。因此,子进程找不到文件ls,从而发出FileNotFoundError

    你可以安装Microsoft's Bash on Windows,它会给你ls。

    但是,pythonic 和更便携的列出目录的方法不是首先使用子进程,而是使用内置的os.listdir

    import os
    print(os.listdir('../input'))
    

    【讨论】:

    • 是的,你说得很好,这行得通,但是你能分析一下为什么 subprocess 模块不起作用吗?谢谢
    • 子流程模块工作正常。您的文件系统上没有 ls 二进制文件。因此,子进程找不到ls 文件,从而发出FileNotFoundError
    • 是的,我认为你是对的,我运行的所有示例都来自官方网站,都是基于 POSIX,这就是为什么调用都失败了,因为 Windows 只是不遵循 POSIX。哈哈
    • 如果您使用的是 os 包,我们可以使用 print(os.listdir('input')) 。
    【解决方案3】:

    我碰巧有同样的问题。在 Windows 上的 Jupiter Notebook 上使用 Phihag 提供的以下代码对我很有用。谢谢。

        import os
        print(os.listdir('../input'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      相关资源
      最近更新 更多