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

2016 年 5 月 24 日
 meteor2013
比如有个 sh 文件 test.sh , 运行每次只能输入一个句子:
$ echo 'this is the first sentence' | test.sh


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

!source test.sh
playniuniu
2016 年 5 月 25 日
python 有个第三方库就叫做 sh , pip install sh 自己看看官方文档,非常方便
sleshep
2016 年 5 月 31 日
popen

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

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

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

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

© 2021 V2EX