网友通过本文主要向大家介绍了白酒执行标准有几种,行政执行手段有几种,执行完毕有几种情况,监督执行有几种形态,常用的shell有哪几种等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
几种执行shell的方法
介绍一下python执行shell命令的四种方法:
1、os模块中的os.system()这个函数来执行shell命令
注,这个方法得不到shell命令的输出。
2、popen()#这个方法能得到命令执行后的结果是一个字符串,要自行处理才能得到想要的信息。
这样得到的结果与第一个方法是一样的。
3、commands模块#可以很方便的取得命令的输出(包括标准和错误输出)和执行状态位
commands.getstatusoutput(cmd)返回(status,output)
commands.getoutput(cmd)只返回输出结果
commands.getstatus(file)返回ls -ld file 的执行结果字符串,调用了getoutput,不建议使用这个方法。
4、subprocess模块
使用subprocess模块可以创建新的进程,可以与新建进程的输入/输出/错误管道连通,并可以获得新建进程执行的返回状态。使用subprocess模块的目的是替代os.system()、os.popen()、commands.等旧的函数或模块。
import subprocess 1、subprocess.call(command, shell=True) #会直接打印出结果。 2、subprocess.Popen(command, shell=True) ###也可以是 subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) #这样就可以输出结果了。
如果command不是一个可执行文件,shell=True是不可省略的。
shell=True意思是shell下执行command
这四种方法都可以执行shell命令。
pysftp 友好的命令执行与交互工具
- import pysftp with pysftp.Connection('hostname', username='me', password='secret') as sftp: with sftp.cd('public'):
- # temporarily chdir to public
- sftp.put('/my/local/filename') # upload file to public/ on remote
- sftp.get('remote_file')
- # get a remote file
所有方法
- dir(pysftp.Connection) ['class', 'del', 'delattr', 'dict', 'doc', 'enter', 'exit', 'format',
- 'getattribute', 'hash', 'init', 'module', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof',
- 'str', 'subclasshook', 'weakref', 'set_authentication', 'set_logging', 'set_username', 'sftp_connect', '_start_transport',
- 'active_ciphers', 'active_compres