[英]Python: Move files to folder based on Part of File name
Folder contains files文件夹包含文件
Now i want to move (1,2,3)(4,5)(6,7) to each folder.现在我想将 (1,2,3)(4,5)(6,7) 移动到每个文件夹。 Based on file name folder has to be created and respective files to be moved.必须根据文件名创建文件夹并移动相应的文件。 The following code works but file name it has in character range x:y but that will not works above file name examples.SO need some modification in file name pass.Thanks.以下代码有效,但文件名在字符范围 x:y 中,但在文件名示例上方不起作用。所以需要对文件名进行一些修改。谢谢。
import os, shutil
os.chdir("<abs path to desktop>")
for f in os.listdir("folder"):
folderName = f[0:10]
if not os.path.exists(folderName):
os.mkdir(folderName)
shutil.copy(os.path.join('folder', f), folderName)
else:
shutil.copy(os.path.join('folder', f), folderName)
Assumption being file will always end with "_number"假设文件将始终以“_number”结尾
import os, shutil
os.chdir("<abs path to desktop>")
for f in os.listdir("folder"):
folderName = f[0:-2]
if not os.path.exists(folderName):
os.mkdir(folderName)
shutil.copy(os.path.join('folder', f), folderName)
else:
shutil.copy(os.path.join('folder', f), folderName)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.