python脚本抛出我可以处理的错误

时间:2017-11-30 13:57:17

标签: python

我创建了上面的脚本来从数据库中删除不需要的基因。如果你只使用它一次但我重新运行它,它确实工作得很好,它给我一个错误信息,如: shutil.Error:目标路径' path / rejected_database_genes / gene_A.fa'已存在 显然它会停止。我认为当文件中有两个或更多基因需要删除时会出现问题。 亲切的问候,谢谢你的帮助。

import glob, sys, os, shutil
from Bio import SeqIO, SearchIO
from Bio.SeqRecord import SeqRecord
import argparse

def help_function():
    print 'Hi'
parser = argparse.ArgumentParser()
parser.add_argument('-input_file', '-i',type=str,help='path_to_data')
opts = parser.parse_args()  

def check_file_exists(filepath, file_description):
    if not os.path.exists(filepath):
        print("The " + file_description + " (" + filepath + ") does not exist")
        sys.exit(1)
    else:
        print file_description + " detected"

def remove_empty_files(alleles_files,destination):
    input_handle=open(alleles_files, 'r')
    gene_records=list(SeqIO.parse(input_handle, 'fasta'))

    for gene_record in gene_records:
        #filename=gene_record.id[0]
        #count=0
        if len(gene_record.seq)<5 or 'N'in gene_record.seq:
            print gene_record.id
        elif '-' in gene_record.seq:
            print gene_record.id
            #count+=1
            shutil.move(alleles_files, destination)

def main():
    destination=opts.input_file + '/rejected_database_genes'
    if os.path.exists(destination):
        print 'Folder already exits'
    else:
        os.makedirs(destination)
        print 'Folder has been created'
    files=glob.glob(opts.input_file+'/*.fa')
    #print files
    #sys.exit()     
    for f in files:
        #print f
        #sys.exit()
        alleles_files=glob.glob(f)[0]
        #print alleles_files
        #sys.exit()         
        remove_empty_files(alleles_files,destination)
    print 'Files have been removed'
main()

2 个答案:

答案 0 :(得分:0)

您遇到的问题出在True行 - 如果您要指定源和目标的完整路径,这将覆盖现有文件,如果您不想,则不会出现此错误覆盖并需要两个文件,只需将目标文件重命名为其他文件。

答案 1 :(得分:0)

我想要的是脚本发现删除该文件的时刻,以及其他地方的存储,以便我稍后再查看。我遇到的问题是,如果在同一个文件中有两个不需要的seq,它会通过我并错误告诉我该文件已存在于目标,并停止。所以我设法通过添加if语句来解决这个问题。更正后的脚本如下:

return combineReducers({
        nav: navReducer,
        signUpReducer : signUpReducer,
    });

通过添加第三个“if”语句,一旦找到不需要的序列,就会删除该文件,将文件移除到目标位置,然后移动以检查下一个文件。