Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: regexp and filenames

Reply
Thread Tools

Re: regexp and filenames

 
 
hokiegal99
Guest
Posts: n/a
 
      07-09-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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> --On 8 Tuesday, July 2003 20:24 -0400 hokiegal99
> <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Padraig@Linux.ie
Guest
Posts: n/a
 
      07-09-2003
hokiegal99 wrote:
> mv test1- "test1*"


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

Pádraig.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
problem with filenames, Filenames and FILENAMES B.J. HTML 4 04-23-2005 08:13 PM
regexp for parsing image filenames out of html code Georg Daniel Vassilopulos Perl Misc 2 08-30-2003 09:56 PM



Advertisments