背景
在 python 脚本内执行 shell 命令,可以解放双手。
使用技巧
一、os.popen(“command”)
语法:
1
os.popen("command")
实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import os
print("os.popen('ls'): {}".format(os.popen("ls")))
print("-" * 20)
pipeline = os.popen("ls")
print("pipeline: {}".format(pipeline))
print("-" * 20)
print("type(pipeline): {}".format(type(pipeline)))
print("-" * 20)
print("pipeline.read(): {}".format(pipeline.read()))
print("-" * 20)
print("type(pipeline.read()): {}".format(type(pipeline.read())))
运行结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
os.popen('ls'): <os._wrap_close object at 0x7fb853f29550>
--------------------
pipeline: <os._wrap_close object at 0x7fb853f50350>
--------------------
type(pipeline): <class 'os._wrap_close'>
--------------------
pipeline.read(): online_notice.py
process_demo.py
saas-collection.json
saas-env-lssonline.json
saas-env-online.json
saas-env-test.json
saas-globals.json
saas_online_report.html
saas_test_report.html
--------------------
type(pipeline.read()): <class 'str'>
分析:os.popen() 无返回值,若需要返回值,需要实例化一个文件对象 pipeline = os.popen(“command”),通过 pipeline.read() 方法读取文件内容。通常用于打开一个管道执行单个命令行。
二、subprocess.call()
语法:
1
subprocess.call("command", shell=True)
实例:
1
2
3
import subprocess
subprocess.call("ls", shell=True)
运行结果:
1
0
分析:subprocess.call()有返回值,0为正常退出。跟 os.popen() 的场景一样,适用于打开一个管道执行单个命令行。
三、os.system()
语法:
1
os.system("cd 指定的目录路径 && 执行的命令")
实例:
1
2
3
import os
os.system("cd ../ && ls")
运行结果:
1
0
分析:os.system(0 有返回值,0为正常退出。使用场景为在指定的目录下执行命令。