how to move files based on file-ending from dirs and subdirs to specific dir?

Evan Carmi evan at
Tue Jan 2 08:14:38 CET 2007


i am trying to move files with a specific file-ending (.msf) to dir above their
current location. my code so far works as long as all the files are on the same
dir level.

but how can i make it work if there are multiple subdirs with files inside of
them on different dir levels?

my code so far is:
top = 'f:\\test\\mail'

import os, time, itertools

#walk and find all files
allfiles = []
for root, dirs, files in os.walk(top, topdown=False):
    for name in files:
        allfiles.append(os.path.join(root, name))
#remove all non .msf files
index = []	
for match in allfiles:
  if match.endswith('.msf'):

indexdest = []	
indexdest = ['%s\\..\\..\\%s\\%s\\%s' % (x , time.strftime('%Y%m%d%H%M%S'),
os.path.basename(os.path.normpath(x+'\\..')), os.path.basename(x)) for x in ind

#\\.. to remove the ending and than basename to add it back on
indexdest = [os.path.normpath(i) for i in indexdest]
indexdest = [i.replace('Mail', 'backups-msf') for i in indexdest]

for a, b in itertools.izip(index, indexdest):
  os.renames(a, b)

i want the .msf files to be in a dir on the same level as where the top string
points to (in this case f:\test\backups-msf).


p.s. is there a more accurate word for a directory level or something on the
same "tier" of a directory?

More information about the Python-list mailing list