os.system与os.popen的用法

def decompress(zipFilePath,csvDir):
    if platform.system() == "Windows":
        cmd = '7z.exe x %s -o%s' % (zipFilePath, csvDir)
    else:
        cmd = '7za x %s -o%s' % (zipFilePath, csvDir)
    rst = os.system(cmd)
    if rst == 0:
        logger.info('%s decompress ok %s'%(zipFilePath,cmd))
    else:
        logger.info('%s decompress Error %s' % (dirs + os.path.sep + filename,cmd))
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.

这个方法会直接返回执行结果,以及状态码。不过官方建议使用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(),可以从该文件对象中读取返回结果。如果执行成功,不会返回状态码,如果执行失败,则会返回错误信息。这里官方也表示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']

猜你喜欢

转载自blog.csdn.net/weixin_41893060/article/details/80858882
今日推荐