Python调用系统命令os.system()和os.popen()的实现
作者:KeithTt 发布时间:2021-06-27 23:56:29
作为一门脚本语言,写脚本时执行系统命令可以说很常见了,python提供了相关的模块和方法。
os模块提供了访问操作系统服务的功能,由于涉及到操作系统,它包含的内容比较多,这里只说system和popen方法。
>>> import os
>>> dir(os)
['DirEntry', 'F_OK', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']
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(子进程)来执行系统命令。
官方说法:
On Unix, the return value is the exit status of the process encoded in the format specified for wait().
The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function.
这个方法只返回状态码,执行结果会输出到stdout,也就是输出到终端。不过官方建议使用subprocess模块来生成新进程并获取结果是更好的选择。
>>> os.system('ls')
access.log douban.py mail.py myapp.py polipo proxychains __pycache__ spider.py test.py users.txt
0
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.popen('ls')
<os._wrap_close object at 0x7f93c5a2d780>
>>> os.popen('la')
<os._wrap_close object at 0x7f93c5a37588>
>>> /bin/sh: la: command not found
>>> f = os.popen('ls')
>>> type(f)
<class 'os._wrap_close'>
读取执行结果:
>>> f.readlines()
['access.log\n', 'douban.py\n', 'import_test.py\n', 'mail.py\n', 'myapp.py\n', 'polipo\n', 'proxychains\n', '__pycache__\n', 'spider.py\n', 'test.py\n', 'users.txt\n']
这里使用os.popen来获取设备号,使用os.system来启动macaca服务(有时间了将macaca的一些经历写写吧)。
两者的区别是:
(1)os.system(cmd)的返回值只会有0(成功),1,2
(2)os.popen(cmd)会把执行的cmd的输出作为值返回。
参考:
https://docs.python.org/3/library/os.html#os.system
https://docs.python.org/3/library/os.html#os.popen
来源:https://www.cnblogs.com/keithtt/p/7709445.html
猜你喜欢
- 如何显示一个等待或欢迎信息? <% Response.Buffer = True %
- PHP getDocNamespaces() 函数实例返回 XML 文档的根节点中声明的命名空间:<?php $xml=<<
- 本文实例讲述了PHP闭包定义与使用。分享给大家供大家参考,具体如下:<?phpfunction getClosure($i){ &nb
- matplotlib官方除了提供了鼠标十字光标的示例,还提供了同一图像内多子图共享光标的示例,其功能主要由widgets模块中的MultiC
- 今天用FrontPage2003,无意中发现一个bug,稍加研究,基本发现这个bug的规律了首先是我的系统版本和Frontpage版本:我的
- 从09年初接手淘宝手机网站前端开发的工作至今,转眼已是一年。一步步看着手机淘宝从最初的beta版本到今天的样子,感慨良多。手机网站开发,有着
- 方法一:(by yangedie )这几天刚刚做了这个东西,有网友问到,所以分享一下。ie6、firefox2 通过,麻烦有ie7的网友测试
- 本文介绍了python同时替换多个字符串方法示例,分享给大家,具体如下:import rewords = ''' &
- 有时一些网页对源码进行了加密,我们很难找到类似像“onselectstart="return false"”这样的代码,
- public static char doVerify(String id) { char pszSrc[]=id.toCharArray(
- 功能很简单,代码也很简洁,这里就不多废话了。package mainimport ( "fmt
- 一:需重定义神经网络继续训练的方法1.训练代码import numpy as npimport tensorflow as tfx_data
- PHP quotemeta() 函数实例在预定义的字符前添加反斜杠:<?php$str = "Hello world. (c
- yaml简单介绍YAML是一种标记语言,它使用空白符号缩进和大量依赖外观的特色,特别适合用来表达或编辑数据结构、各种配置文件、倾印调试内容、
- apache对php的支持是通过apache的mod_php5模块来支持的,这点与nginx不同。nginx是通过第三方的fastcgi处理
- Web,全称为 World Wide Web,是 Internet 上最重要和最为人们所熟知的应用之一。Web 是指 Internet 上所
- 本文实例讲述了Python实现读取txt文件中的数据并绘制出图形操作。分享给大家供大家参考,具体如下:下面的是某一文本文件中的数据。6.11
- 本文实例讲述了PHP5.6读写excel表格文件操作。分享给大家供大家参考,具体如下:测试环境:php5.6.24.这块没啥兼容问题。需要更
- python中字符串数组如何逆序排列?下面给大家介绍几种方法:1、数组倒序:原始元素的倒序排列(1)切片>>> arr =
- LOAD DATA INFILE '文件地址' INTO TABLE 表名 FIELDS TERMINATED BY