Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python subprocess.Popen()错误(没有这样的文件或目录)_Python_Subprocess_System_Popen - Fatal编程技术网

Python subprocess.Popen()错误(没有这样的文件或目录)

Python subprocess.Popen()错误(没有这样的文件或目录),python,subprocess,system,popen,Python,Subprocess,System,Popen,我试图使用Python函数计算文件中的行数。在当前目录中,虽然os.system(“ls”)查找文件,但命令subprocess.Popen([“wc-l filename”],stdout=subprocess.PIPE)不起作用 这是我的密码: >>> import os >>> import subprocess >>> os.system("ls") sorted_list.dat 0 >>> p = subproc

我试图使用Python函数计算文件中的行数。在当前目录中,虽然
os.system(“ls”)
查找文件,但命令
subprocess.Popen([“wc-l filename”],stdout=subprocess.PIPE
)不起作用

这是我的密码:

>>> import os
>>> import subprocess
>>> os.system("ls")
sorted_list.dat
0
>>> p = subprocess.Popen(["wc -l sorted_list.dat"], stdout=subprocess.PIPE)File "<stdin>", line 1, in <module>
File "/Users/a200/anaconda/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
File "/Users/a200/anaconda/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
导入操作系统 >>>导入子流程 >>>操作系统(“ls”) 排序列表.dat 0 >>>p=subprocess.Popen([“wc-l sorted_list.dat”],stdout=subprocess.PIPE)文件“”,第1行,在 文件“/Users/a200/anaconda/lib/python2.7/subprocess.py”,第710行,在__ 错误读取,错误写入) 文件“/Users/a200/anaconda/lib/python2.7/subprocess.py”,第1335行,在执行子进程中 引发子对象异常 OSError:[Errno 2]没有这样的文件或目录
您应该将参数作为列表传递(推荐):

否则,如果要将整个
“wc-l sorted_list.dat”
字符串用作命令,则需要传递
shell=True


请阅读有关安全问题的详细信息。

发生错误的原因是您试图运行名为
wc-l sorted_list.dat的命令,也就是说,它试图查找名为
“/usr/bin/wc-l sorted dat”的文件

分开你的论点:

["wc", "-l", "sorted_list.dat"]
subprocess.Popen("wc -l sorted_list.dat", shell=True, stdout=subprocess.PIPE)
["wc", "-l", "sorted_list.dat"]