2016-03-17 71 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

感謝哥們......它正在工作 –

相關問題