如何将特定文件移动到匹配的文件夹中

时间:2013-01-28 17:46:33

标签: python file select move directory

我已经编写了一个脚本来帮助我将所需的文件移动到从文件列表中创建的相应文件夹中。我知道描述很模糊,但我的脚本是:

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

这个脚本的目的是允许我根据每个文件文件名自动将大量文件组织成sperate文件夹

1 个答案:

答案 0 :(得分:0)

shutil模块具有您想要的功能。