av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Python調用系統命令os.system()和os.popen()的實現

瀏覽:4日期:2022-06-30 17:00:04

作為一門腳本語言,寫腳本時執行系統命令可以說很常見了,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.txt0os.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.logn’, ’douban.pyn’, ’import_test.pyn’, ’mail.pyn’, ’myapp.pyn’, ’polipon’, ’proxychainsn’, ’__pycache__n’, ’spider.pyn’, ’test.pyn’, ’users.txtn’]

這里使用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.systemhttps://docs.python.org/3/library/os.html#os.popen

到此這篇關于Python調用系統命令os.system()和os.popen()的實現的文章就介紹到這了,更多相關Python os.system()和os.popen()內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 毛片av在线| 国产精品久久久久久妇女6080 | 亚洲福利视频一区 | 久久久久久一区 | 国产a久久麻豆入口 | 亚洲精品国产精品国自产观看浪潮 | 九九超碰 | 欧美亚洲激情 | 在线亚洲欧美 | 高清乱码男女免费观看 | 夜夜嗨av一区二区三区网页 | 国产日韩欧美一区二区 | 久久久久蜜桃 | 巨骚综合 | 日本一级淫片色费放 | 欧美日韩精品在线 | 日本精品网站 | 青青草视频网站 | 久久在线免费视频 | 色六月婷婷 | 国产乱码久久久久久 | 三级视频在线播放 | 国产精品一区二区不卡 | 久久福利网 | 天天操夜夜爽 | 亚洲怡春院 | 69av在线| 成人三级在线观看 | 欧美精品99 | 日韩在线视频免费观看 | 国产精品2| 日韩欧美影院 | 日韩一区二区三区精品 | 亚洲一区二区三区在线视频 | 国产又粗又大又硬 | 91欧美激情一区二区三区成人 | 中文字幕免费高清 | 国产免费一区二区三区免费视频 | 久久久久久网 | 中文字字幕 | 日韩在线观看中文字幕 |