2016-03-17 70 views
-2

我想从测试文件夹中读取所有fasta文件,并将文件的名称放在单个文件的所有标头中。代码为第一个文件工作,并不继续第二个文件并返回错误。你能帮我找到我的代码中的错误或编辑它。谢谢从文件夹中读取所有文件并编辑

import sys, glob, os, string 
header = '' 
check = 0 
path = "./test/" 
dirs = os.listdir(path) 
for file in dirs: 
    fp = open(file, "r") 
    fpx = open('%s_output.txt' % file, 'w') 
    for line in fp: 
     if line.startswith('>'): 
      line = line.rstrip() 
      check = check + 1 
      if check >= 1: 
       header = line 
       fpx.write(header + '_' + file + '\n') 
     else: 
      line = line.rstrip() 
      fpx.write(line + '\n') 
+0

请提供运行代码时返回的错误。 – mobiuseng

回答

0

这将是很好,提供您收到的错误消息!我认为这一定会因为“找不到文件”而失败,因为您尝试按名称而不是路径打开文件。尝试fp = open(os.path.join(path, file), "r")

import sys, glob, os, string 
header = '' 
check = 0 
path = "./test/" 
dirs = os.listdir(path) 
for file in dirs: 
    fp = open(os.path.join(path, file), "r") 
    fpx = open('%s_output.txt' % file, 'w') 
    for line in fp: 
     if line.startswith('>'): 
      line = line.rstrip() 
      check = check + 1 
      if check >= 1: 
       header = line 
       fpx.write(header + '_' + file + '\n') 
     else: 
      line = line.rstrip() 
      fpx.write(line + '\n') 
+0

感谢哥们......它正在工作 –

相关问题