python调用其它程序执行命令行指令 os.system和os.popen
在python脚本中调用其他程序,或执行命令行指令,可以用os.system,os.popen,subprocess.popen这三种方式。这三种方式所适用的情况各不相同。区别在于调用程序后执行的操作,函数返回的是调用程序的输出,还是程序运行的状态码。
1.os.system
需要等待子进程执行完再继续执行的用这个
原型:
os.system(command)
command --- 调用的命令
该函数创建子进程调用其他程序,并在父进程中wait()子进程结束,command调用的程序产生输出,将会被打印在屏幕上(stdout),函数返回值是指令或程序执行的状态码。该函数通常用于一些简单的命令执行。
参考文档
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. If command generates any output, it will be sent to the interpreter standard output stream.
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.
On Windows, the return value is that returned by the system shell after running command. The shell is given by the Windows environment variable COMSPEC: it is usually cmd.exe, which returns the exit status of the command run; on systems using a non-native shell, consult your shell documentation.
The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.
Availability: Unix, Windows.
import os res = os.system("echo \"helloworld\"")
2.os.popen
想要起另外一个程序,两个程序并行执行的用这个
原型:
os.popen(command,mode,buf)
command --- 调用的命令
mode --- 模式权限可以是 'r'(默认) 或 'w'
bufsize -- 指明了文件需要的缓冲大小:0意味着无缓冲(默认),1意味着行缓冲,其它正值表示缓冲区大小,负的bufsize表示使用系统的默认值(0)。
该函数创建子进程调用其他程序,父进程不会wait()子进程结束,而是在调用os.popen()之后继续执行,command调用程序产生输出,将会通过文件对象返回,该函数返回值是一个文件对象。可以对该文件对象进行读f.read()写f.write()操作,获取程序输出或者对程序输入。该函数不会获取程序状态码。
参考文档
os.popen(cmd, mode='r', buffering=-1)
Open a pipe to or from command cmd. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. The buffering argument has the same meaning as the corresponding argument to the built-in open() function. The returned file object reads or writes text strings rather than bytes.
The close method returns None if the subprocess exited successfully, or the subprocess’s return code if there was an error. On POSIX systems, if the return code is positive it represents the return value of the process left-shifted by one byte. If the return code is negative, the process was terminated by the signal given by the negated value of the return code. (For example, the return value might be - signal.SIGKILL if the subprocess was killed.) On Windows systems, the return value contains the signed integer return code from the child process.
This is implemented using subprocess.Popen; see that class’s documentation for more powerful ways to manage and communicate with subprocesses.
import os f = os.popen("echo \"helloworld\"") f.read()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具