将python中的双引号shell命令传递给subprocess.Popen()? [英] Passing double quote shell commands in python to subprocess.Popen()?

查看:83
本文介绍了将python中的双引号shell命令传递给subprocess.Popen()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试传递一个命令,该命令仅适用于命令行中 ffmpeg 的 "concat:file1|file2" 参数周围的文字双引号.

I've been trying to pass a command that works only with literal double quotes in the commandline around the "concat:file1|file2" argument for ffmpeg.

但是,我无法使用 subprocess.Popen() 从 python 完成这项工作.任何人都知道如何将引号传递给 subprocess.Popen?

I cant however make this work from python with subprocess.Popen(). Anyone have an idea how one passes quotes into subprocess.Popen?

代码如下:

command = "ffmpeg -i "concat:1.ts|2.ts" -vcodec copy -acodec copy temp.mp4"

output,error = subprocess.Popen(command, universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()

当我这样做时,ffmpeg 除了在 concat 段周围使用引号外,不会采取任何其他方式.有没有办法将这一行成功传递给 subprocess.Popen 命令?

When I do this, ffmpeg won't take it any other way other than quotes around the concat segement. Is there a way to successfully pass this line to subprocess.Popen command?

推荐答案

我建议使用列表形式的调用而不是引用的字符串版本:

I'd suggest using the list form of invocation rather than the quoted string version:

command = ["ffmpeg", "-i", "concat:1.ts|2.ts", "-vcodec", "copy",
           "-acodec", "copy", "temp.mp4"]
output,error  = subprocess.Popen(
                    command, universal_newlines=True,
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

这更准确地表示将要传递到最终进程的确切参数集,并消除了与 shell 引用混淆的需要.

This more accurately represents the exact set of parameters that are going to be passed to the end process and eliminates the need to mess around with shell quoting.

也就是说,如果您绝对想使用纯字符串版本,只需使用不同的引号(和 shell=True):

That said, if you absolutely want to use the plain string version, just use different quotes (and shell=True):

command = 'ffmpeg -i "concat:1.ts|2.ts" -vcodec copy -acodec copy temp.mp4'
output,error  = subprocess.Popen(
                    command, universal_newlines=True, shell=True,
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

这篇关于将python中的双引号shell命令传递给subprocess.Popen()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆