gpt4 book ai didi

python - 子进程 python filenotfounderror : [winerror 2]

转载 作者:太空狗 更新时间:2023-10-29 20:57:54 25 4
gpt4 key购买 nike

我一直在使用 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

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

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

最佳答案

此代码运行 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'))

关于python - 子进程 python filenotfounderror : [winerror 2],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43088388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com