python - 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!
解决方案
推荐阅读
- android - React-native:调用 onLongPress(Prop of TouchableOpacity) 需要多少毫秒?
- ios - 在 iOS Playground 上使用 AVFoundation 的 Recorder,但是 recorder.record() 总是返回 false
- javascript - 使用 vue js 多选动态更改选项
- servlets - 如何在不编译的情况下在 groovy-servlet 中使用模板方法?
- android - 如何使用数据绑定更新进度条百分比
- php - 是否可以仅显示 10 页 pdf 中的 2 页?
- java - 如何以编程方式在android中的数组中添加可绘制的所有图像
- .net - 在 Azure Web App Service 上,有时会得到 Character �(#65533),但随后一切恢复正常,它们是同一页面
- javascript - 卡在 javascript 中读取 CSV 文件
- postgresql - 如何在 postgresql 中创建 n=3 的 btree 索引?