我有一个包含在这个名称格式不同的子目录的文件名列表的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