我試圖創建一個腳本,該腳本從我的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
這就是我爲什麼在這裏試火的原因。 –