处理 os.system 的结果
Handle result of os.system
我正在使用 python 编写功能脚本,但我无法处理此命令行的结果:
os.system("ps aux -u %s | grep %s | grep -v 'grep' | awk '{print }'" % (username, process_name)
它显示了 pid,但我不能将它用作列表。
如果我测试:
pids = os.system("ps aux -u %s | grep %s | grep -v 'grep' | awk '{print }'" % (username, process_name)
print type(pids)
#Results
29719
30205
31037
31612
<type 'int'>
为什么 pids
是 int
?我如何处理此结果 List
?
陌生人部分:
print type(os.system("ps aux -u %s | grep %s | grep -v 'grep' | awk '{print }'" % (username, process_name))
什么都没有。我的控制台上没有写任何类型..
os.system
不捕获它运行的命令的输出。为此,您需要使用 subprocess
.
from subprocess import check_output
out = check_output("your command goes here", shell=true)
以上内容适用于 Python 2.7。对于年龄较大的 Python,请使用:
import subprocess
p = subprocess.Popen("your command goes here", stdout=subprocess.PIPE, shell=True)
out, err = p.communicate()
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.
如果您想访问命令的输出,请改用 subprocess module,例如check_output
:
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)
Run command with arguments and return its output as a byte string.
我正在使用 python 编写功能脚本,但我无法处理此命令行的结果:
os.system("ps aux -u %s | grep %s | grep -v 'grep' | awk '{print }'" % (username, process_name)
它显示了 pid,但我不能将它用作列表。
如果我测试:
pids = os.system("ps aux -u %s | grep %s | grep -v 'grep' | awk '{print }'" % (username, process_name)
print type(pids)
#Results
29719
30205
31037
31612
<type 'int'>
为什么 pids
是 int
?我如何处理此结果 List
?
陌生人部分:
print type(os.system("ps aux -u %s | grep %s | grep -v 'grep' | awk '{print }'" % (username, process_name))
什么都没有。我的控制台上没有写任何类型..
os.system
不捕获它运行的命令的输出。为此,您需要使用 subprocess
.
from subprocess import check_output
out = check_output("your command goes here", shell=true)
以上内容适用于 Python 2.7。对于年龄较大的 Python,请使用:
import subprocess
p = subprocess.Popen("your command goes here", stdout=subprocess.PIPE, shell=True)
out, err = p.communicate()
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.
如果您想访问命令的输出,请改用 subprocess module,例如check_output
:
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)
Run command with arguments and return its output as a byte string.