我有一個包含在這個名稱格式不同的子目錄的文件名列表的txt文件列表寫入輸出到子目錄的位置中的文件通過文件名列表中的行併產生適當的輸出。根據輸入文件路徑的
我想要的是將具有不同名稱的文件的輸出保存在與文件名列表中提供的路徑對應的目錄中。
我寫的代碼指示(對於每個for
迴路1)目錄中的從該腳本在命令行作爲這樣的」 shell$ python script.py inputfilelist.txt
運行所有的輸出這是我的腳本:
import sys
with open(sys.argv[1]) as f:
for filename in f:
with open(filename.strip().strip("\n"),'a') as f1:
#print f1
output = []
outfilename = filename.strip("\n").lstrip("./").replace("/", "__") + "out.txt"
#print outfilename
with open(outfilename, 'a') as outfile:
line = f1.readline()
while line and not line.startswith('GO-ID'):
line = f1.readline()
data = f1.readlines()
for line in data:
line = line.split("\t")
GOnr = line[0].lstrip("\s")
pvalue = line[1].strip()
corrpval = float(line[2].strip())
if corrpval <= 0.05:
outstring = "GO:"+"%s %s" % (GOnr, str(corrpval))
outfile.write(outstring + "\n")
#print outstring
我正在尋找最簡單的方法讓每個循環保存其在OUTFILE相同的文件名的輸入路徑的位置。
想我必須使用sys
模塊,b ut閱讀python提供的解釋,我不太明白如何使用sys.stdin
sys.stdout
函數。
相反,我一直在試圖通過定義一個函數upfront來重新格式化文件列表中的輸入目錄,爲每個新的out.txt
文件生成完整路徑。
def output_name(input_file):
file_line=inputfile.strip()
line_as_list=file_line.split("/")
line_as_list.append("out.txt") # file name
line_as_list.remove(line_as_list[-2]) # remove file name of input file from path description
full_output_name="/".join(line_as_list) #join to add leading and intermittent `/`
return full_output_name
當我交互運行這段代碼,它做什麼,它需要太多,如:outputname("./A_blurb/test.txt") == "./A_blurb/out.txt"
然而,當我在命令行中運行它,我得到這個消息:return full_output_name \n SyntaxError: 'return' outside function
我仔細檢查縮進但無法找到這個錯誤消息的原因是什麼.... 謝謝。
請告訴我們您的代碼。 – Alfe 2014-10-11 20:59:44