FileNotFoundError:系统找不到指定的文件

时间:2017-08-28 22:04:08

标签: python python-3.x

我正在尝试列出输入文件夹中的文件:

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

我收到此错误(使用Python 3):

C:\Users\Tristan\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)
    988                                          env,
    989                                          cwd,
--> 990                                          startupinfo)
    991             finally:
    992                 # Child is launched. Close the parent's copy of those pipe

FileNotFoundError: [WinError 2] The system cannot find the file specified

2 个答案:

答案 0 :(得分:1)

ls在Windows中不作为命令存在,因此出错;它找不到要运行的名为ls的可执行文件。这个类似问题的解决方案可以帮助您做您想做的事:How to use Subprocess in Windows

答案 1 :(得分:0)

感谢Izaak Weiss回答,解决方案变为:

import os
l = os.listdir("input")
print (l)