我试图通过使用下面代码中的subprocess模块来运行python shell命令,但是我不知道为什么我的脚本会抛出下面这样的错误。有人能帮助我吗?
Traceback (most recent call last):
File "/Scripts/test_file.py", line 6, in <module>
p2 = subprocess.Popen('sed s\'/"/ /g\'', stdin=p1.stdout, stdout=subprocess.PIPE)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: "sed s'/"/ /g'"`
import subprocess
#output3.txt='/Users/boggulv/Desktop/output3.txt'
p1 = subprocess.Popen( ['cat', 'output3.txt'], stdout=subprocess.PIPE)
print(p1)
p2 = subprocess.Popen('sed s\'/"/ /g\'', stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen('grep "sO"', stdin=p2.stdout,stdout=subprocess.PIPE)
p4 = subprocess.Popen('grep -v "get"', stdin=p3.stdout, stdout=subprocess.PIPE)
p5 = subprocess.Popen('cut -d \',\' -f2', stdin=p4.stdout, stdout=subprocess.PIPE)
p6 = subprocess.Popen('sed \'s/"//g\'', stdin=p5.stdout, stdout=subprocess.PIPE)
p7 = subprocess.Popen('sort', stdin=p6.stdout, stdout=subprocess.PIPE)
p8 = subprocess.Popen('sort', stdin=p8.stdout, stdout=subprocess.PIPE)
p9 = subprocess.Popen('uniq -c', stdin=p8.stdout, stdout=subprocess.PIPE)
p0 = subprocess.Popen('sort -nr', stdin=p9.stdout, stdout=subprocess.PIPE)
print(p01.communicate())
已尝试更改为列表。
p2 = subprocess.Popen('sed \'s/"/ /g\'', stdin=p1.stdout, stdout=subprocess.PIPE, shell=True)
p3 = subprocess.Popen(['grep','"shipOption"'], stdin=p2.stdout,stdout=subprocess.PIPE,shell = True)
p4 = subprocess.Popen(['grep','-v', '"getShipMethod"'], stdin=p3.stdout, stdout=subprocess.PIPE,shell = True)
p5 = subprocess.Popen(['cut','-d','\',\'', '-f2'], stdin=p4.stdout, stdout=subprocess.PIPE,shell = True)
p6 = subprocess.Popen(['sed','\'s/"//g\''],stdin=p5.stdout, stdout=subprocess.PIPE,shell = True)
p7 = subprocess.Popen(['sort'], stdin=p6.stdout, stdout=subprocess.PIPE,shell = True)
p8 = subprocess.Popen(['uniq', '-c'], stdin=p7.stdout, stdout=subprocess.PIPE,shell = True)
p9 = subprocess.Popen(['sort', '-nr'], stdin=p8.stdout, stdout=subprocess.PIPE,shell = True)
p0 = subprocess.Popen(['head', '-10'], stdin=p9.stdout, stdout=subprocess.PIPE,shell = True)```
New Error:
`usage: grep [-abcdDEFGHhIiJLlMmnOopqRSsUVvwXxZz] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
usage: grep [-abcdDEFGHhIiJLlMmnOopqRSsUVvwXxZz] [-A num] [-B num] [-C[num]]
[-e pattern] [-f file] [--binary-files=value] [--color=when]
[--context[=num]] [--directories=action] [--label] [--line-buffered]
[--null] [pattern] [file ...]
usage: cut -b list [-n] [file ...]
cut -c list [file ...]
cut -f list [-s] [-w | -d delim] [file ...]
(b'', None)
cat: stdin: Input/output error`
3条答案
按热度按时间wwwo4jvm1#
你的命令仍然是错误的。如果你只是想像shell那样运行这些命令,绝对最简单的方法是...使用shell。
如果您想删除
shell=True
并手动运行所有这些进程,您必须了解shell的工作原理,特别是,您需要修改引号,以便您运行的命令在shell处理完语法引号后 * 仍保留引号。请特别注意
sed
和grep
的参数是如何去掉它们的外引号的,以及我们是如何在所有地方都去掉shell=True
的。(或其他subprocess
方法)是一个列表,则不应使用shell=True
,反之亦然。(有些情况下,您可以将列表传递给shell=True
,但是......我们甚至还没有开始讨论这个问题。)不过,所有这些似乎都没有实际意义,因为Python可以出色地完成所有这些事情。
也许您希望将
rstrip
和replace
放在if
中以避免不必要的工作,当然,可以对shell管道进行相同的重构。brgchamk2#
根据Gordon上面的描述-默认情况下,Popen()处理
作为要运行的命令的名称,而不是命令名加一个参数。因此,您需要执行以下操作之一:
或
使用shell=True可以让Popen函数将字符串拆分为包含命令和参数的列表。
还要注意,p0是最后一个结果,但您调用了p01.communicate()
neekobn83#
假设您使用命令'pythonhello.py--main'来运行hello.py文件,并将--main作为参数。
这将抛出一个错误,即没有找到这样的文件或目录。
在子流程中,您可以将其编写为
您可以将其放入变量中,并按如下所示打印从脚本返回的值