(python) issue bash commands from within python script (alla perl system($cmd))
Within a python script, I want to issue开发者_高级运维 a command. In perl, I could define a command, save it as a variable (here, $cmd
) then type system($cmd)
and then the command is executed.
How can i do that in python?
You can use os.system()
, but prefer subprocess
instead.
Another good choice is "commands" module: http://docs.python.org/library/commands.html.
you can use os.system(), or the newer subprocess module. Other possible alternatives (for older Python versions) include these. (eg os.spawn*,os.popen*,etc)
Lastly, try using Python's modules to do operating system stuff instead of calling external commands if possible, unless its a third party tool you are executing and Python doesn't have the api.
精彩评论