win python 运行bash脚本_从运行Python中的bash脚本

I need to run a bash script from Python. I got it to work as follows:

import os

os.system("xterm -hold -e scipt.sh")

That isn't exactly what I am doing but pretty much the idea. That works fine, a new terminal window opens and I hold it for debugging purposes, but my problem is I need the python script to keep running even if that isn't finished. Any way I can do this?

解决方案

I recommend you use subprocess module: docs

And you can

import subprocess

cmd = "xterm -hold -e scipt.sh"

# no block, it start a sub process.

p = subprocess.Popen(cmd , shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# and you can block util the cmd execute finish

p.wait()

# or stdout, stderr = p.communicate()

For more info, read the docs,:).

edited misspellings

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值