more regexp

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

  1. hokiegal99

    hokiegal99 Guest

    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!
    hokiegal99, Jul 11, 2003
    #1
    1. Advertising

  2. hokiegal99

    John Hunter Guest

    >>>>> "hokiegal99" == hokiegal99 <> 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
    John Hunter, Jul 11, 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. Michael
    Replies:
    4
    Views:
    400
    Matt Hammond
    Jun 26, 2006
  2. Robert Klemme

    With a Ruby Yell: more, more more!

    Robert Klemme, Sep 28, 2005, in forum: Ruby
    Replies:
    5
    Views:
    206
    Jeff Wood
    Sep 29, 2005
  3. Greg Hurrell
    Replies:
    4
    Views:
    152
    James Edward Gray II
    Feb 14, 2007
  4. Mikel Lindsaar
    Replies:
    0
    Views:
    466
    Mikel Lindsaar
    Mar 31, 2008
  5. Joao Silva
    Replies:
    16
    Views:
    343
    7stud --
    Aug 21, 2009
Loading...

Share This Page