Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > more regexp

Reply
Thread Tools

more regexp

 
 
hokiegal99
Guest
Posts: n/a
 
      07-11-2003
I can replace all spaces in filenames and directory names with this bit
of code: (thanks to all who helped me get this!)

> fspaces = re.compile('[ ]')
>
> for root, dirs, files in os.walk('/test/dir'):
> for file in files:
> badchars = fspaces.findall(file) #find spaces
> 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)



> dspaces = re.compile('[ ]')
>
> for root, dirs, files in os.walk('/test/dir'):
> for dir in dirs:
> badchars = dspaces.findall(dir) #find spaces
> newdir = ''
> for badchar in badchars:
> newdir = dir.replace(badchar,'%')
> if newdir:
> newpath = os.path.join(root,newdir)
> oldpath = os.path.join(root,dir)
> os.rename(oldpath,newpath)


Now, I'd like to be able to search for the '%' character at the begining
and the ending of filenames and dirs and remove them entirely. Could
someone suggest a way to do this? Any tips are welcomed!

 
Reply With Quote
 
 
 
 
John Hunter
Guest
Posts: n/a
 
      07-11-2003
>>>>> "hokiegal99" == hokiegal99 <(E-Mail Removed)> writes:

hokiegal99> I can replace all spaces in filenames and directory
hokiegal99> names with this bit of code: (thanks to all who helped
hokiegal99> me get this!)

Sorry if I'm missing something since I'm jumping into this thread
late, but if all you want to do is replace spaces, why do you need
regexps? Can't you just use string.replace?

hokiegal99> Now, I'd like to be able to search for the '%'
hokiegal99> character at the begining and the ending of filenames
hokiegal99> and dirs and remove them entirely. Could someone
hokiegal99> suggest a way to do this? Any tips are welcomed!

>>> fname = '%Hi%mom%'
>>> fname.strip('%')

'Hi%mom'

See also lstrip and rstrip to remove chars (default whitespace) from
the beginning or end of a string (strip does both beginning and end).

JDH

 
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
new RegExp().test() or just RegExp().test() Matěj Cepl Javascript 3 11-24-2009 02:41 PM
[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
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments