我试图创建一个脚本,该脚本从我的Ubuntu服务器调用linux命令并将上述命令的输出打印到txt文件。这实际上是我写过的第一个脚本,我刚刚开始学习python。我想在3个独立的文件夹中的3个文件与文件名唯一的日期。捕获来自外部命令的输出并将其写入文件
def swo():
from subprocess import call
call("svn info svn://url")
def tco():
from subprocess import call
call("svn info svn://url2")
def fco():
from subprocess import call
call("url3")
import time
timestr = time.strftime("%Y%m%d")
fs = "/path/1/" + timestr
ft = "/path/2/" + timestr
fc = "/path/3/" + timestr
f1 = open(fs + '.txt', 'w')
f1.write(swo)
f1.close()
f2 = open(ft + '.txt', 'w')
f2.write(tco)
f2.close()
f3 = open(fc + '.txt' 'w')
f3.write(fco)
f3.close()
它在f.write()函数失败。我被困在使得linux命令的输出成为新文件中的实际文本。
'subprocess'通过将打开的文件对象传递给'call'函数的'stdout'参数,可以直接将命令输出写入文件。 https://docs.python.org/2/library/subprocess.html – FamousJameous
你可能想从一个[Python教程]开始(https://docs.python.org/3.5/tutorial/)。你的函数没有返回一个(有用的)值,并且你不是首先调用它们。 ('tco()',而不是'tco')。 – chepner
这就是我为什么在这里试火的原因。 –