如何在python上运行sh脚本?

时间:2019-09-08 02:52:53

标签: python bash python-2.7 window

说我有一个python程序,我想执行此操作

grep "username" accounts.txt

1行

accounts.txt与我的py文件位于同一文件夹中。我知道在C中有一些像System(grep“ username” accounts.txt)的函数,并且会想知道在python中是否有类似的函数。通常,由于python很大,因此python读入account.txt的速度太慢。但是在bash或linux中它的速度要快得多,所以想知道我是否可以使用bash查找用户名行,然后python将其打印出来。

如果没有,那么将是一种有效的方法来集成一个大的用户名文件,我可以在我的python代码中调用该文件来打印出与之相关的行。

2 个答案:

答案 0 :(得分:2)

while($row=mysqli_fetch_array($stmt))

while ($stmt->fetch())
{
?>
  <option value="<?php echo htmlentities($WritterId); ?>"><?php echo htmlentities($Writter); ?></option>
<?php
}

应该可以工作。我还没有使用过,但是我知道(出于某种原因)使用子过程会更好。

import os
os.system('grep username accounts.txt')

输出:Hello World!

import subprocess
subprocess.call('grep username accounts.txt', shell=True)

输出:Hello World!

答案 1 :(得分:0)

使用commands模块或subprocess模块执行命令。

注意:命令模块已从python3中删除。因此,如果您使用的是python3,我建议您使用subprocess模块​​,因为python2具有两个模块。