Unexpected output while walking dirs

Evan Carmi evan at binarymanipulations.com
Tue Jan 2 11:50:18 CET 2007

wittempj <at> hotmail.com <martin.witte <at> gmail.com> writes:

> If I do get this correct - you have files like \test\Mail\somename.msf
> and \test\Mail\somedirectory\someothername.msf, these files you want to
> move to \test\backup\timestamp\somename.msf and
> \test\backup\timestamp\somedirectory\someothername.msf.
> In your code you start with collecting allfiles, and then you remove
> all except *,msf files. You can do this in one step to, by collecting
> only the wanted files in a listby applying the glob command from module
> glob :
> allfiles = []
> for root, dirs, files in os.walk(top, topdown=False):
>     targetfiles = glob.glob(os.path.join(root,'*.msf'))
>     allfiles += targetfiles
> Now allfiles contains the list of .msf files down from directory
> specified in variable top. From here I would create a list of 2-tuples,
> with as first element the full original filename, and as second element
> the desired backupname. Note that in your code you call the clock in a
> for loop, this might result in more directories.
> backuproot = os.path.join(os.path.dirname(top), 'backup-msf',
> time.strftime('%Y%m%d%H%M%S'))
> backupfilenames = []
> for f in allfiles:
>     backupfilenames.append((f, os.path.join(backuproot,
> os.path.basename(f))))
> >From here it is easy to do the move.
Thanks so much for your help, you got me started and I finished it with some
help from others as well. For anyone interested the working code is:

#Author: Evan Carmi
#Date: 20060102
#Purpose: To uncorrupt Mozilla Thunderbird mail index files.
#Version: 1.00
import os, time, glob
srcRoot = 'f:\\test\\mail'
backupRoot = os.path.join(os.path.dirname(srcRoot), 'backup-msf',
for root, dirs, files in os.walk(srcRoot, topdown=False):
    sources = glob.glob(os.path.join(root,'*.msf'))
    pairs = []
    for source in sources:
        # If srcRoot is /foo/bar and source is /foo/bar/baz, let relativeSource
equal /baz
        #let relativeSource be equal to the remainder of source when you take
away len(srcRoot)
        idx = len(srcRoot)
        relativeSource = source[idx:]
        # Then let destination equal /quux/baz if backupRoot is /quux
        destination = backupRoot + relativeSource
        # relativeSource begins with a path separator, so os.path.join will
misinterpret it.
        pair = (source, destination)
    for pair in pairs:
        # This is functionally equivalent to os.renames(pair[0], pair[1])


Thanks, Evan

More information about the Python-list mailing list