Python subprocess arguments

╄→гoц情女王★ 提交于 2020-01-21 03:01:20

问题


For example I am using ffplay and want to run this command -bufsize[:stream_specifier] integer (output,audio,video)

At the moment I have this:

subprocess.call(["ffplay", "-vn", "-nodisp","-bufsize 4096", "%s" % url])

But this says it is invalid.


回答1:


As JBernardo mentioned in a comment, separate the "-bufsize 4096" argument into two, "-bufsize", "4096". Each argument needs to be separated when subprocess.call is used with shell=False (the default). You can also specify shell=True and give the whole command as a single string, but this is not recommended due to potential security vulnerabilities.

You should not need to use string formatting where you have "%s" % url. If url is a string, pass it directly, otherwise call str(url) to get a string representation.




回答2:


This is the way to go:

url = 'http://www.whatever.com'
cmd = 'ffplay -vn -nodisp -bufsize 4096 '.split()
subprocess.call(cmd + [str(url)], shell=False)



回答3:


While using shlex.split() is overkill for your use case, many of the comments seem to be asking about the use of spaces in parameters in cases where a CLI allows you to pass in quoted strings containing spaces (i.e. git commit -m "Commit message here").

Here is a quick python function that can be used to run commands including parameters with spaces:

import shlex, subprocess

def run_command( command ):
    subprocess.call(shlex.split(command))


来源:https://stackoverflow.com/questions/11679936/python-subprocess-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!