0
我试图从多个文本文件中创建一个文件我跨越多个文件夹。这是我的代码拼接。它的工作原理只有在程序文件被放置在每个文件夹中:通过多个文件夹连接多个文件
import os
file_list = [each for each in cur_folder if each.endswith(".txt")]
print file_list
align_file = open("all_the_files.txt","w")
seq_list = []
for each_file in file_list:
f_o = open(file_path,"r")
seq = (f_o.read().replace("\n",""))
lnth = len(seq)
wholeseq = ">"+each_file+" | "+str(lnth)+" nt\n"+seq+"\n"
align_file.write(wholeseq)
print "done"
现在,我尝试过编辑,以确保它在整个Data
文件夹会自动运行,然后进入子目录并连接所有文件有没有我将程序文件粘贴到每个文件夹中。这是编辑。
import os
dir_folder = os.listdir("C:\Users\GAMER\Desktop\Data")
for each in dir_folder:
cur_folder = os.listdir("C:\\Users\\GAMER\\Desktop\\Data\\"+each)
file_list = []
file_list = [each for each in cur_folder if each.endswith(".txt")]
print file_list
align_file = open("all_the_files.txt","w")
seq_list = []
for each_file in file_list:
f_o = open(file_path,"r")
seq = (f_o.read().replace("\n",""))
lnth = len(seq)
wholeseq = ">"+each_file+" | "+str(lnth)+" nt\n"+seq+"\n"
align_file.write(wholeseq)
print "done" , cur_folder
然而,当我运行它,我得到的文件夹说没有这样的文件存在的文件的错误。我似乎明白为什么,特别是因为它命名了不是“硬编码”的文件。任何帮助将不胜感激。
如果代码看起来很丑,可随意提出更好的方法来实现它。
看看[os.walk(https://docs.python.org/2/library/os.html#os.walk) – 2014-10-29 17:04:34
只是确保你知道你正在重塑车轮:[猫](https://en.wikipedia.org/wiki/Cat_%28Unix%29) – goncalopp 2014-10-29 17:05:40
@goncalopp猫是unix - 他的文件路径显示他在Windows上。 – furkle 2014-10-29 17:06:59