python:我可以将基于名称一部分的文件移动到具有该名称的文件夹中吗 [英] python: can i move a file based on part of the name to a folder with that name

查看:57
本文介绍了python:我可以将基于名称一部分的文件移动到具有该名称的文件夹中吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录,其中包含大量文件,我想根据文件名的一部分移入文件夹.我的文件列表如下:

I have a directory with a large number of files that I want to move into folders based on part of the file name. My list of files looks like this:

  • ID1_geneabc_species1.fa

  • ID1_geneabc_species1.fa

ID1_genexy_species1.fa

ID1_genexy_species1.fa

ID2_geneabc_species1.fa

ID2_geneabc_species1.fa

ID3_geneabc_species2.fa

ID3_geneabc_species2.fa

ID3_genexy_species2.fa

ID3_genexy_species2.fa

ID4_genexy_species3.fa

ID4_genexy_species3.fa

我想根据文件名的最后一部分(species1,种类2,种类3)将拥有的文件移动到单独的文件夹中.文件名的开头部分不一定总是具有相同的数字和/或字母,而是始终由3个部分组成,并用下划线"_"分隔.

I want to move the files I have into separate folders based on the last part of the file name (species1, species2, species3). The first parts of the file name do not always have the same number of numbers and/or letters but are always in 3 parts separated by an underscore '_'.

这是我从网上查看时尝试过的方法,但是它不起作用:

This is what I have tried from looking online but it does not work:

import os
import glob

dirs = glob.glob('*_*')

files = glob.glob('*.fa')

for file in files:
   name = os.path.splitext(file)[0]
   matchdir = next(x for x in dirs if name == x.rsplit('_')[0])
   os.rename(file, os.path.join(matchdir, file))

在下面的脚本的列表中,我具有名称列表(species1,种类2,种类3),这些名称与文件名的第三部分相对应.我可以使用这些名称中的每一个在当前工作目录中创建一组目录.在以下脚本之后是否有更好的方法来执行此操作,例如循环浏览物种列表,匹配文件,然后将其移至正确的目录?谢谢.

I have the list of names (species1, species2, species3) in a list in the script below, which correspond to the third part of my file name. I am able to create a set of directories in my current working directory from each of these names. Is there be a better way to do this after the following script, like looping through the list of species, matching the file, then moving it into the correct directory? THANKS.

from Bio import SeqIO
import os
import itertools

#to get a list of all the species in genbank file
all_species = []
for seq_record in SeqIO.parse("sequence.gb", "genbank"):
    all_species.append(seq_record.annotations["organism"])

#get unique names and change from set to list
Unique_species = set(all_species)
Species = list(Unique_species)

#send to file
f = open('speciesnames.txt', 'w')
for names in Species:
    f.write(names+'\n')
f.close()

print ('There are ' + str(int(len(Species))) + ' species.')

#make directory for each species
path = os.path.dirname(os.path.abspath(__file__))
for item in itertools.product(Species):
    os.makedirs(os.path.join(path, *item))

推荐答案

因此,您需要一个函数,该函数从文件中获取文件夹名称.然后,您遍历文件,创建不存在的目录,然后将文件移到那里.这样的东西应该可以解决.

So, you want a function, which gets folder name from file. Then you iterate over files, create dirs which don't exist and move files there. Stuff like that should work out.

def get_dir_name(filename):
    pos1 = filename.rfind('_')
    pos2 = filename.find('.')
    return filename[pos1+1:pos2]

for f in glob.glob('*.fa'):
    cwd = os.getcwd()
    dir_name = cwd+'/'+get_dir_name(f)
    print dir_name
    if not os.path.exists(dir_name):
        os.mkdir(dir_name)
    os.rename(f, dir_name+'/'+f)

这篇关于python:我可以将基于名称一部分的文件移动到具有该名称的文件夹中吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆