Python执行cmd命令

来源:互联网 发布:武威免费制作seo 编辑:程序博客网 时间:2024/05/22 12:08

需求:

需要在python的脚本中运行ffmpeg命令。比如需要执行以下命令: ffmpeg -i test hello.mp4 test hello.flv

如果使用os.system命令,传递进去就是os.system("ffmpeg -i test hello.mp4 test hello.flv"),注意,文件名中有空格,os.system()还是用空格把字符串给拆成了不同的部分,然后再调用shell来执行。


参考博客:http://blog.csdn.net/harbinzju/article/details/7857229


查看os.system文档:

os.system(command) 
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command.
On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.
On Windows, the return value is that returned by the system shell after running command, given by the Windows environment variable COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always 0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.
The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.

官方是建议我们使用subprocess.Popen()来代替os.system(),先mark,继续学习中。

0 0
原创粉丝点击