os.system, os.popen, os.startfile区别

OS.SYSTEM的使用说明
>>> help(os.system)
Help on built-in function system in module nt:

system(command)
    Execute the command in a subshell.  

os.system()是在当前进程中打开一个子shell(子进程)来执行系统命令。
os.system(command) 文档链接

os模块中的system()函数可以方便地运行其他程序或者脚本。其函数原型为:
os.system(command)
command 为要执行的命令,近似于Windows下cmd窗口中输入的命令。
如果要向程序或者脚本传递参数,可以使用空格分隔程序及多个参数。

os.popen() 的使用说明
help(os.popen)
Help on function popen in module os:

popen(cmd, mode='r', buffering=-1)
    # Supply os.popen()

cmd:要执行的命令。
mode:打开文件的模式,默认为'r',用法与open()相同。
buffering:0意味着无缓冲;1意味着行缓冲;其它正值表示使用参数大小的缓冲。负的bufsize意味着使用系统的默认值,一般来说,对于tty设备,它是行缓冲;对于其它文件,它是全缓冲。

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 close method returns None if the subprocess exited successfully, or the subprocess’s return code if there was an error. 

This is implemented using subprocess.Popen;

这个方法会打开一个管道,返回结果是一个连接管道的文件对象,该文件对象的操作方法同open(),可以从该文件对象中读取返回结果。如果执行成功,不会返回状态码,如果执行失败,则会将错误信息输出到stdout,并返回一个空字符串。这里官方也表示subprocess模块已经实现了更为强大的subprocess.Popen()方法。

os.startfile() 的使用说明
os.startfile(file)

os.startfile实现双击运行程序

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容