因此,我创建的脚本需要遍历服务器上的所有文件,然后运行每个文件名,然后运行命令“ll”,然后将输出命令并将其打印到txt文件。对Python文件运行Bash命令
例如:
夹/ FILENAME.TXT LL
输出:SoMETHINGSomethingSomethingother - 这被发送到output.txt的文件
文件夹/子文件夹/ filename3.txt LL
输出:SoMETHINGSomethingSomethingother - 这是发送到output.txt文件
这是我到目前为止:
import os
with open("output.txt", "w") as a:
for path, subdirs, files in os.walk(r'C:\Users\user\Desktop\Test_Py'):
for filename in files:
f = os.path.join(filename)
m = f + ' ll'
a.write(str(m) + os.linesep)
所以我想弄清楚现在是如何使打印出来的文件名与“ll”命令运行。到目前为止,此代码会将该文件夹及其子文件夹中的所有文件的名称写入到我的output.txt文件中。
有没有人有任何想法?
检查'subprocess'模块http://docs.python.org/library/subprocess.html – avasal
可能重复[从python运行shell命令并捕获输出](http://stackoverflow.com/questions/4760215/run-shell-command-from-python-and-capturing-the-output) –
值得注意的是,在Perl中,你可以通过打开文件来写入流水线(例如'open(my $ pipeline,“| command | command > file“);'),但是Python不允许这样做。 –