2016-07-24 184 views
1

我需要運行多個輸入文件的Python腳本,併爲每一個,我想生成(用於input_16jun.txt比如我要輸出的文件是16jun_output.txt)新的相應的輸出文件。我試着做這樣的事情:輸出重定向到多個文件

nohup python script.py input_{16..22}jun.txt > {16..22}jun_output.txt & 

但我不斷收到「模棱兩可的重定向」錯誤。有誰知道如何解決這一問題?還是有其他更好的方法?

+0

試試這個 - nohup的蟒蛇script.py輸入_ {} 16..22 jun.txt | tee {16..22} jun_output.txt& –

+0

仍然是同樣的錯誤:/ – ion20

+0

剛剛更新。試試吧,如果它工作 –

回答

2

在循環使用bash應該像這樣每個輸入文件。

for f in input_*.txt; do python script.py $f > "${f:6:-4}"_output.txt; done 

或者,如果你想在python腳本中做循環。

import glob 
import os 

input_files = glob.glob("input_*.txt") 

for f in input_files: 
    os.system("python script.py {} > {}_output.txt".format(f,f.split("input_")[1].rstrip(".txt"))) 

如果你想運行並行(而不是按順序)script.py你也可以考慮使用Python multiprocessing包。