用来执行shell指令
1、os.system()
system(command) -> exit_status
Execute the command (a string) in a subshell.
import os result = os.system(‘ls /‘) print(result) print(type(result)) 输出: bin boot data dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var 0 #只打印不保存输出内容, 返回执行状态,成功为0 <type ‘int‘>
2、os.popen()
popen(command [, mode=‘r‘ [, bufsize]]) -> pipe
Open a pipe to/from a command returning a file object.
import os result = os.popen(‘ls /‘) for line in result.readlines(): print(line) print(type(result)) 输出: #保存输出内容,返回一个‘file’ home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var <type ‘file‘>
3、
时间: 2024-10-11 06:52:16