请问怎样用 Python 去执行 sh 文件比较好呢??

2016-05-24 14:49:26 +08:00
 meteor2013
比如有个 sh 文件 test.sh , 运行每次只能输入一个句子:
$ echo 'this is the first sentence' | test.sh


如果有 100 个句子,我想用 Python 去循环运行这个 test.sh ,应该在 Python 里面怎样执行 sh 文件呢?
3608 次点击
所在节点    Python
9 条回复
xiaket
2016-05-24 15:35:51 +08:00
循环调 sh 这个库
meteor2013
2016-05-24 15:39:05 +08:00
@xiaket 能说具体点吗
dphdjy
2016-05-24 15:42:27 +08:00
keyword: Python shell subprocess
congeec
2016-05-24 15:50:43 +08:00
其实用 shell 最简单
python 里面 os.system 也简单粗暴
``` Python
for i in sentences:
os.system('echo ' + i + ' | test.sh')
```
aristotll
2016-05-24 20:44:33 +08:00
@congeec os.system 不推荐用了吧
congeec
2016-05-24 21:26:57 +08:00
@aristotll 自己用的一次性脚本,又没啥安全问题
quxw
2016-05-24 22:46:26 +08:00
ipython

!source test.sh
playniuniu
2016-05-25 08:55:02 +08:00
python 有个第三方库就叫做 sh , pip install sh 自己看看官方文档,非常方便
sleshep
2016-05-31 13:15:46 +08:00
popen

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/280904

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX