开发者

(python) issue bash commands from within python script (alla perl system($cmd))

开发者 https://www.devze.com 2022-12-19 19:04 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号