Re: regexp and filenames

Discussion in 'Python' started by hokiegal99, Jul 9, 2003.

  1. hokiegal99

    hokiegal99 Guest

    I added a few chars to the bad character list and these two print
    statements to the end of the script:

    print oldpath
    print newpath

    So I could see what filenames were changed and where. I wrote a script
    that generates broken filenames in the filesystem and then ran your
    script to see if everything works as expected. Did some testing... so
    far it works great. See output below.

    Thanks for the program! I only wanted direction, but you went the
    extra mile! Also, I d/l 2.3b2 before testing your script. No problems
    there either.

    mv test1- "test1*"
    mv test2- "test2<"
    mv test3- "test3>"
    mv test4- "test4:"
    mv test5- "test5;"
    mv test6-t "test6\t"
    mv test7- "test7?"
    mv test8- "test8|"
    mv test9-t "test9\"t"
    mv test10- "test10="
    mv test11- "test11+"
    #---------
    cd SPREADSHEETS
    #---------
    mv test1- "test1*"
    mv test2- "test2<"
    mv test3- "test3>"
    mv test4- "test4:"
    mv test5- "test5;"
    mv test6-t "test6\t"
    mv test7- "test7?"
    mv test8- "test8|"
    mv test9-t "test9\"t"
    mv test10- "test10="
    mv test11- "test11+"
    #---------
    #--------
    cd ../BEN*/NCAA*R*
    #--------
    mv test1- "test1*"
    mv test2- "test2<"
    mv test3- "test3>"
    mv test4- "test4:"
    mv test5- "test5;"
    mv test6-t "test6\t"
    mv test7- "test7?"
    mv test8- "test8|"
    mv test9-t "test9\"t"
    mv test10- "test10="
    mv test11- "test11+"
    #---------

    #---------
    #1 = *
    #2 = <
    #3 = >
    #4 = :
    #5 = ;
    #6 = \
    #7 = ?
    #8 = |
    #9 = "
    #10 = =
    #11 = +







    emf <> wrote in message news:<>...
    > --On 8 Tuesday, July 2003 20:24 -0400 hokiegal99
    > <> wrote:
    >
    > > I have a bit of a problem that I think Python can solve, but I need a
    > > bit of direction. I'm replacing 12 Macs with PCs. Macs allow
    > > characters in filenames that Windows does not, specifically the
    > > following:
    > >
    > > : * ? " < > | / \
    > >
    > > I would like to write a script that searches through a directory for
    > > filenames that contain any of the aboved listed characters and
    > > replaces them with a - (the minus sign).
    > >
    > > I'm familiar with regexp, but I've only used it in Python to act on
    > > the contents of a file, not filenames in a directory. Any pointers on
    > > how to approach this are welcome!
    > >

    >
    > This should do the trick. You can steal walk from 2.3 and use it in 2.2
    > with hardly any modifications, if you're using 2.2.
    >
    > import os, re
    >
    > # | and \ have to be escaped;
    > # the r prefix on the string keeps it from being a huge escape:
    > badcharset = re.compile(r'[*:?"<>/\|\\]')
    >
    > for root, dirs, files in os.walk('/'): #or however you spell root on
    > your mac
    > for file in files:
    > badchars = badcharset.findall(file)
    > newfile = ''
    > for badchar in badchars:
    > newfile = file.replace(badchar,'-')
    > if newfile:
    > newpath = os.path.join(root,newfile)
    > oldpath = os.path.join(root,file)
    > os.rename(oldpath,newpath)
    >
    > Hope that helps,
    >
    > ~mindlace
    > http://mindlace.net
     
    hokiegal99, Jul 9, 2003
    #1
    1. Advertising

  2. hokiegal99

    Guest

    hokiegal99 wrote:
    > mv test1- "test1*"


    don't forget to handle the case where test1- already exists.

    Pádraig.
     
    , Jul 9, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. B.J.
    Replies:
    4
    Views:
    747
    Toby Inkster
    Apr 23, 2005
  2. Greg Hurrell
    Replies:
    4
    Views:
    164
    James Edward Gray II
    Feb 14, 2007
  3. Mikel Lindsaar
    Replies:
    0
    Views:
    496
    Mikel Lindsaar
    Mar 31, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    368
    7stud --
    Aug 21, 2009
  5. Georg Daniel Vassilopulos

    regexp for parsing image filenames out of html code

    Georg Daniel Vassilopulos, Aug 30, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    98
    Alan J. Flavell
    Aug 30, 2003
Loading...

Share This Page