regexp and filenames

hokiegal99 hokiegal99 at hotmail.com
Wed Jul 9 10:15:32 EDT 2003


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 <i at mindlace.net> wrote in message news:<mailman.1057723392.15805.python-list at python.org>...
> --On 8 Tuesday, July 2003 20:24 -0400 hokiegal99 
> <hokiegal99 at hotmail.com> 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




More information about the Python-list mailing list