我已经编写了一个脚本来帮助我将所需的文件移动到从文件列表中创建的相应文件夹中。 我知道描述很模糊,但我的脚本是:
import os
from os import listdir
import dircache
import sys
import shutil
import dircache
import glob
#selecting the directory to work from
path = "C:\Users\hra\Desktop\fg"
dirList=os.listdir(path)
#print file names in directory
for fname in dirList:
print fname
myList2 = sorted(dirList, key=lambda x:x[0:3])
oldList = [x[0:2] for x in myList2]
newList = list(set(oldList))
print "newList =", newList
#first check
#print "my list = ", myList2
#print "my list letters = ", oldList
print "unique characters = ", newList
root_path = "C:\Users\hra\Desktop\fg"
folders = newList
for folder in folders:
os.mkdir(os.path.join(root_path,folder))
#selecting unique names from the list of files
from itertools import groupby
the_list = oldList
gb = groupby(sorted(the_list))
print [(i,len(list(j))) for i,j in gb]
#counters for checking
#fCounter = len(glob.glob1("C:\Users\hra\Desktop\fg","fg*"))
#mCounter = len(glob.glob1("C:\Users\hra\Desktop\fg","m*"))
#bCounter = len(glob.glob1("C:\Users\hra\Desktop\fg","b*"))
nLCounter = len(newList)
#print "unique character count =", nLCounter
#print "f = ", fCounter
#print "m = ", mCounter
#print "b = ", bCounter
此脚本的要点是允许我根据每个文件文件名自动将多个文件组织到单独的文件夹中
shutil 模块具有执行所需操作的函数。