Python 根据名称移动文件
Python Move Files Based On Name
值得称赞,我目前使用的代码来自 cji、here.
的回复
我正在尝试从源文件夹中递归提取所有文件,并将它们移动到文件名前五个字符的文件夹中 0:5
我的代码如下:
import os
import shutil
srcpath = "SOURCE"
srcfiles = os.listdir(srcpath)
destpath = "DESTINATION"
# extract the three letters from filenames and filter out duplicates
destdirs = list(set([filename[0:5] for filename in srcfiles]))
def create(dirname, destpath):
full_path = os.path.join(destpath, dirname)
os.mkdir(full_path)
return full_path
def move(filename, dirpath):
shutil.move(os.path.join(srcpath, filename)
,dirpath)
# create destination directories and store their names along with full paths
targets = [(folder, create(folder, destpath)) for folder in destdirs]
for dirname, full_path in targets:
for filename in srcfiles:
if dirname == filename[0:5]:
move(filename, full_path)
现在,用下面的代码更改 srcfiles = os.listdir(srcpath)
和 destdirs = list(set([filename[0:5] for filename in srcfiles]))
得到一个变量中的路径和文件名的前五个字符另一个。
srcfiles = []
destdirs = []
for root, subFolders, files in os.walk(srcpath):
for file in files:
srcfiles.append(os.path.join(root,file))
for name in files:
destdirs.append(list(set([name[0:5] for file in srcfiles])))
我将如何着手修改原始代码以使用它...或者如果有人对我将如何着手这样做有更好的想法。谢谢。
我真的不能很容易地测试它,但我认为这段代码应该可以工作:
import os
import shutil
srcpath = "SOURCE"
destpath = "DESTINATION"
for root, subFolders, files in os.walk(srcpath):
for file in files:
subFolder = os.path.join(destpath, file[:5])
if not os.path.isdir(subFolder):
os.makedirs(subFolder)
shutil.move(os.path.join(root, file), subFolder)
值得称赞,我目前使用的代码来自 cji、here.
的回复我正在尝试从源文件夹中递归提取所有文件,并将它们移动到文件名前五个字符的文件夹中 0:5
我的代码如下:
import os
import shutil
srcpath = "SOURCE"
srcfiles = os.listdir(srcpath)
destpath = "DESTINATION"
# extract the three letters from filenames and filter out duplicates
destdirs = list(set([filename[0:5] for filename in srcfiles]))
def create(dirname, destpath):
full_path = os.path.join(destpath, dirname)
os.mkdir(full_path)
return full_path
def move(filename, dirpath):
shutil.move(os.path.join(srcpath, filename)
,dirpath)
# create destination directories and store their names along with full paths
targets = [(folder, create(folder, destpath)) for folder in destdirs]
for dirname, full_path in targets:
for filename in srcfiles:
if dirname == filename[0:5]:
move(filename, full_path)
现在,用下面的代码更改 srcfiles = os.listdir(srcpath)
和 destdirs = list(set([filename[0:5] for filename in srcfiles]))
得到一个变量中的路径和文件名的前五个字符另一个。
srcfiles = []
destdirs = []
for root, subFolders, files in os.walk(srcpath):
for file in files:
srcfiles.append(os.path.join(root,file))
for name in files:
destdirs.append(list(set([name[0:5] for file in srcfiles])))
我将如何着手修改原始代码以使用它...或者如果有人对我将如何着手这样做有更好的想法。谢谢。
我真的不能很容易地测试它,但我认为这段代码应该可以工作:
import os
import shutil
srcpath = "SOURCE"
destpath = "DESTINATION"
for root, subFolders, files in os.walk(srcpath):
for file in files:
subFolder = os.path.join(destpath, file[:5])
if not os.path.isdir(subFolder):
os.makedirs(subFolder)
shutil.move(os.path.join(root, file), subFolder)