[英]How to run complex python commands using subprocess using relative reference?
I am trying to execute this command using Python: 我正在尝试使用Python执行以下命令:
findSyntax = "find . -maxdepth 2 -name '.config' | cpio -updm ../test1/"
subprocess.Popen(findSyntax.split(' '))
But this command just would not work. 但是此命令将不起作用。 When I execute this command, it will start listing all the files (not just .config) under the .
当我执行此命令时,它将开始列出。下的所有文件(而不仅仅是.config)。 directory beyond the maxdepth 2... which is a long list.
超过maxdepth 2 ...的目录,这是一个很长的列表。
What am I missing here! 我在这里想念什么! Can someone point it out?
有人可以指出吗? Thanks.
谢谢。
NOTE: I've tried running subProcess.run
as well with same results. 注意:我已经尝试运行
subProcess.run
以及相同的结果。 I was able to get just the find part working using os.system()
command. 使用
os.system()
命令,我只能找到查找部分。
EDIT: I just wanted to clarify that this command will copy the files found with the exact directory structure intact to the new location (creating subdirectories if necessary). 编辑:我只是想澄清一下,此命令会将具有完整目录结构的文件复制到新位置(如有必要,创建子目录)。 I've tried this command on bash terminal, and it works fine.
我已经在bash终端上尝试过此命令,并且效果很好。 But I couldn't get it to work with Python.
但是我无法使其与Python一起使用。
EDIT2: So, the whole command works with
os.system()
, but I couldn't figure out how to make it work withsubprocess
.EDIT2:那么,整个命令可
os.system()
但我无法弄清楚如何使它与工作subprocess
。os.system()
is supposed to be deprecated, so I would be very interested in figuring out the solution usingsubprocess
instead.os.system()
应该已被弃用,因此我对使用subprocess
os.system()
解决方案非常感兴趣。
Please look at this good answer and this also helps 请看看这个好答案 , 这也有帮助
But in essence, you can't use your above subprocess command with a pipe. 但实质上,您不能将上述子过程命令与管道一起使用。
Lets run through the simple example of getting all py
files in the current directory: ( ls | grep py
) 让我们看一下获取当前目录中所有
py
文件的简单示例:( ls | grep py
)
This is broken: 这已破了:
import subprocess
subprocess.call(['ls', '|', 'grep', 'py'])
Because subprocess does only one process at a time, and by piping you are really creating 2 processes. 由于子流程一次只执行一个流程,因此通过管道传输实际上可以创建2个流程。
The simple but limited (to platform) way is to use os.system
简单(但仅限于平台)的方法是使用
os.system
import os
os.system('ls | grep py')
This literally just passes a shell command to the system to execute. 实际上,这只是将shell命令传递给系统来执行。
However, you should do it with subprocess by defining your pipes: 但是,您应该通过定义管道来对子流程进行处理:
# Get all files and pass the stdout to a pipe
p1 = subprocess.Popen(['ls'], stdout=subprocess.PIPE)
# then pass that pipe to another process as stdin and do part 2
output = subprocess.check_output(['grep', 'py'], stdin=p1.stdout)
print(output)
So, a copy paste for your example: 因此,为您的示例粘贴副本:
import subprocess
p1 = subprocess.Popen("find . -maxdepth 2 -name '.config'".split(), stdout=subprocess.PIPE)
output = subprocess.check_output("cpio -updm ../test1/".split(), stdin=p1.stdout)
Or with os
: 或与
os
:
os.system("find . -maxdepth 2 -name '.config' | cpio -updm ../test1/")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.