简体   繁体   中英

how do you move specific files into matching folders

i have already written a script to help me move desired files into corresponding folder that have been made from the list of files. i know that description is quite vague but my script is:

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

the point of this script is to allow me to automatically have a number of files organised into sperate folders based on each files filename

shutil模块具有执行所需功能的功能。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM