Think before you speak, read before you think.

python执行shell命令

在 python for unix and linux system administraton一书中,有这么一句话:

You can run any shell command in Python just as it would be run with Bash.

你可以用python执行bash中可以执行的任何命令。

显示/tmp/信息(两种执行bash命令的方式)

#!/usr/bin/env python
import subprocess
subprocess.call(["ls","-l","/tmp/"])
subprocess.call("df -l /tmp/", shell=True)

将输出赋给指定变量

output = subprocess.check_output('ls', shell=True)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *