[Tutor] Changing files names

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Sep 15 01:30:18 CEST 2004



On Tue, 14 Sep 2004, Bryan wrote:

> >>import glob, os
> >>from sets import Set
> >>skip = Set(['1.2.20.5000.raw',
>             '1.2.27.5001.raw',
>             '1.2.31.5002.raw',
>             ])
>
> >>basedir = r'F:\Python\Renaming\Data'
> >>for fname in glob.glob(basedir + '*.raw'):
>     if fname in skip: continue
>     newname = basedir + 'bryfile.' + fname[-8:]
>     print 'renaming', fname, newname
>     os.rename(fname, newname)

>  So basically, I'm trying to change the file name so it looks like
> bryfile.5000.raw, bryfile..5001.raw, bryfile.5002.raw, and so on.


Hi Bryan,

Ok, sounds good so far.



> This program does not work, and does not print out, and I'm not sure
> why.


This is actually very valuable information.  From the control flow of the
program, this suggests two possibilities:


    1.  All the 'fnames' are in 'skip'.

    2.  glob.glob(basedir + '*.raw') is returning the empty list.

I don't think possibility 1 is actually possible.  *grin* But possibility
2 seems very probable.  Check the return value that you're getting from
the blob again.


I see why it's failing, but you should try to find it too.  If you don't
see the problem in a few minutes, look at the end of this message for
spoilers.


*** Spoiler space ahead ***








*** Spoilers ****

You need to make sure the path separator comes before the globbing '*'.
Instead for asking for all the files from:

     r'F:\Python\Renaming\Data*.rar'

you probably want:

     r'F:\Python\Renaming\Data\*.rar'

instead.



Good luck!



More information about the Tutor mailing list