regexp and filenames

Alan Kennedy alanmk at hotmail.com
Wed Jul 9 07:17:08 EDT 2003


hokiegal99 wrote:

> 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!

I think regular expressions are perhaps a little overkill for this
job, and perhaps inefficient also.

Here is a solution that uses the string.translate() function to do the
same thing.

#==============================
import string

undesired = r""":*?"<>|\/"""

transtab = string.maketrans(undesired, '-' * len(undesired))

filenames = [
    'file1:',
    'file2*',
    'file3?',
    'file4"',
    'file5<',
    'file6>',
    'file7|',
    'file8\\',
    'file9/',
]

for f in filenames:
    print string.translate(f, transtab)
#==============================

I expect, although I haven't timed it, that this will run faster than
a regular expression based solution.

Note the use of a raw string (r"") to contain the undesired
characters, which allows us to use the string escape character (\)
without escaping it. (But even still, it may not appear as the last
character in the string).

Another poster has shown how to navigate directory hierarchies, so
I'll leave it to you to combine the two code snippets.

HTH,

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list