首页 > 解决方案 > Move files with same string part on name to directory

问题描述

I have a directory withe the files following the follow structure Actor Name - title Actor Name 2 - title

I've made a simple python script, that's scan the directory, get the Actor Name, create a folder with the name if doesn't have, and move the file to the directory:

caminho = input('Digite o caminho da pasta a ser organizada: ')
local = input('Digite o novo local organizado: ')
path = caminho+'/'
local = local+'/'
for file in os.listdir(caminho):
    (file, ext) = os.path.splitext(file)
    velho_arquivo = path+file+ext
    novo_arquivo = local+file+ext
    nome_pasta = file.split('-')[0]
    nome_pasta = nome_pasta.rstrip()
    novo_arquivo = local+nome_pasta+'/'+file+ext
    pasta = local+nome_pasta
    print(nome_pasta)
    print(novo_arquivo)
    print(velho_arquivo)
    if not os.path.exists(pasta):
        os.makedirs(pasta)
        shutil.move(velho_arquivo,novo_arquivo)
    else:
        print('Erro ao mover o arquivo')

The problem is, the script move the first file the contain the actor name, and doesn't move the other files that contains the same actor name.

What can be made to copy the others files to the same folder?

Thanks!

标签: pythonpython-3.xos.path

解决方案


推荐阅读