more regexp

John Hunter jdhunter at ace.bsd.uchicago.edu
Thu Jul 10 22:46:09 EDT 2003


>>>>> "hokiegal99" == hokiegal99  <hokiegal99 at hotmail.com> 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





More information about the Python-list mailing list