python 执行shell命令并将结果保存
2017-06-17 12:58
651 查看
方法1: 将shell执行的结果保存到字符串
def run_cmd(cmd):
result_str=''
process = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result_f = process.stdout
error_f = process.stderr
errors = error_f.read()
if errors: pass
result_str = result_f.read().strip()
if result_f:
result_f.close()
if error_f:
error_f.close()
return result_str
方法2: 将shell执行的结果写入到指定文件
def run_cmd2file(cmd):
fdout = open("file_out.log",'a')
fderr = open("file_err.log",'a')
p = subprocess.Popen(cmd, stdout=fdout, stderr=fderr, shell=True)
if p.poll():
return
p.wait()
return
def run_cmd(cmd):
result_str=''
process = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result_f = process.stdout
error_f = process.stderr
errors = error_f.read()
if errors: pass
result_str = result_f.read().strip()
if result_f:
result_f.close()
if error_f:
error_f.close()
return result_str
方法2: 将shell执行的结果写入到指定文件
def run_cmd2file(cmd):
fdout = open("file_out.log",'a')
fderr = open("file_err.log",'a')
p = subprocess.Popen(cmd, stdout=fdout, stderr=fderr, shell=True)
if p.poll():
return
p.wait()
return
相关文章推荐
- python 执行shell命令并将结果保存的实例
- Python实现ssh登录执行shell命令并将结果写入mysql数据库
- windows linux 使用python执行系统命令并将结果保存到变量
- python输出shell命令执行结果
- 获取python执行shell命令的结果
- 在python中执行shell命令,并以字符串形式返回命令执行结果
- 执行shell命令并读取执行结果——popen()的用法
- paip.执行shell cmd 命令uapi java php python总结
- python 使用subprocess模块来执行shell命令
- python学习——python中执行shell命令
- python执行shell命令
- python学习——python中执行shell命令
- makefile中使用shell命令并将结果保存为变量
- java执行shell命令或者脚本,返回结果到程序
- Perl 获取shell命令的执行结果
- paip.执行shell cmd 命令uapi java php python总结
- java调用shell命令并获取执行结果
- 如何将linux 命令的执行结果在屏幕输出的同时保存到文件
- linux使用popen读取shell命令执行结果
- 【vim】在vim中将unix命令执行的结果保存在vim文件中