【发布时间】:2016-12-30 13:39:31
【问题描述】:
我正在尝试创建一个脚本,该脚本从我的 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参数,将命令输出直接写入文件。 docs.python.org/2/library/subprocess.html -
您可能希望从处理Python tutorial 开始。您的函数都没有返回(有用的)值,而且您一开始也没有调用它们。 (
tco(),不是tco)。 -
这就是为什么我在这里,火刑考验。
标签: python linux ubuntu svn scripting