为什么 python 给出一个错误,说它是一个文件夹,但它是一个文件?

why python is giving an error saying that is a folder when it is a file?

我在做一个程序,其中一个功能是根据ID整理一个fasta文件。函数描述为:

def sorting_files(output,my_fasta_file, sample_name):
    #to sort the fasta file that contain all the genes 
    my_file=open(my_fasta_file)
    input_handle=(my_file,'rU')
    fasta_file=SeqIO.parse(input_handle, 'fasta')
    sorted_fasta_file=(record for record in sorted(fasta_file, key=lambda x : x.id))
    sorted_file=SeqIO.write(sorted_fasta_file, output + 'sorted_' + sample_name +'.fa', 'fasta')
    return sorted_file 

然后我从 main 中调用该函数:

      #to sort the fasta file
   def main():    
        folders=sorted(glob.glob(opts.input_file +'/*_velvet'))         
        for folder in folders:
            my_fasta_file=glob.glob(folder +'/H*.fa')
            print my_fasta_file
            #sys.exit()
            sorted_file=sorting_files(my_fasta_file,output,sample_name)
            print 'The fasta file has been sorted, hoooray!'
    main()

When it prints my_fasta_file it prints:

['/home/path_to_file/velvet_file/sample_name_velvet/sample_name.fa']

但我收到以下错误消息:

File "model.py", line 82, in sorting_files
    my_file=open(my_fasta_file)
IOError: [Errno 21] Is a directory: '/home/path_to_files/velvet_file/sample-name_velvet/'

我不知道错误在哪里。对此的任何帮助将不胜感激。 谢谢,

我认为您的代码中的错误与参数的顺序有关。

def sorting_files(output,my_fasta_file, sample_name):
    ...
sorted_file=sorting_files(my_fasta_file,output,sample_name)

你把 output 放在 my_fasta_file 应该去的地方,反之亦然。我不知道你的代码中的 output 是什么,但我猜它是目录 '/home/path_to_files/velvet_file/sample-name_velvet/'