Python调用系统命令os.system()和os.popen()的实现
作为一门脚本语言,写脚本时执行系统命令可以说很常见了,python提供了相关的模块和方法。
os模块提供了访问操作系统服务的功能,由于涉及到操作系统,它包含的内容比较多,这里只说system和popen方法。
>>>importos >>>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) Helponbuilt-infunctionsysteminmodulent: system(command) Executethecommandinasubshell.
从字面意思上看,os.system()是在当前进程中打开一个子shell(子进程)来执行系统命令。
官方说法:
OnUnix,thereturnvalueistheexitstatusoftheprocessencodedintheformatspecifiedforwait().
Thesubprocessmoduleprovidesmorepowerfulfacilitiesforspawningnewprocessesandretrievingtheirresults;usingthatmoduleispreferabletousingthisfunction.
这个方法只返回状态码,执行结果会输出到stdout,也就是输出到终端。不过官方建议使用subprocess模块来生成新进程并获取结果是更好的选择。
>>>os.system('ls') access.logdouban.pymail.pymyapp.pypolipoproxychains__pycache__spider.pytest.pyusers.txt 0
os.popen()
>>>help(os.popen) Helponfunctionpopeninmoduleos: popen(cmd,mode='r',buffering=-1) #Supplyos.popen()
cmd:要执行的命令。
mode:打开文件的模式,默认为'r',用法与open()相同。
buffering:0意味着无缓冲;1意味着行缓冲;其它正值表示使用参数大小的缓冲。负的bufsize意味着使用系统的默认值,一般来说,对于tty设备,它是行缓冲;对于其它文件,它是全缓冲。
官方说法:
Openapipetoorfromcommandcmd.Thereturnvalueisanopenfileobjectconnectedtothepipe,whichcanbereadorwrittendependingonwhethermodeis'r'(default)or'w'.
TheclosemethodreturnsNoneifthesubprocessexitedsuccessfully,orthesubprocess'sreturncodeiftherewasanerror.
Thisisimplementedusingsubprocess.Popen;
这个方法会打开一个管道,返回结果是一个连接管道的文件对象,该文件对象的操作方法同open(),可以从该文件对象中读取返回结果。如果执行成功,不会返回状态码,如果执行失败,则会将错误信息输出到stdout,并返回一个空字符串。这里官方也表示subprocess模块已经实现了更为强大的subprocess.Popen()方法。
>>>os.popen('ls')>>>os.popen('la') >>>/bin/sh:la:commandnotfound >>>f=os.popen('ls') >>>type(f)
读取执行结果:
>>>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
到此这篇关于Python调用系统命令os.system()和os.popen()的实现的文章就介绍到这了,更多相关Pythonos.system()和os.popen()内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。