escaping characters in filenames

MRAB python at
Wed Jul 29 10:22:34 EDT 2009

J Kenneth King wrote:
> I wrote a script to process some files using another program.  One thing
> I noticed was that both os.listdir() and os.path.walk() will return
> unescaped file names (ie: "My File With Spaces & Stuff" instead of "My\
> File\ With\ Spaces\ \&\ Stuff").  I haven't had much success finding a
> module or recipe that escapes file names and was wondering if anyone
> could point me in the right direction.
That's only necessary if you're building a command line and passing it
as a string.

> As an aside, the script is using with the "shell=True"
> parameter.  There isn't really a reason for doing it this way (was just
> the fastest way to write it and get a prototype working).  I was
> wondering if Popen objects were sensitive to unescaped names like the
> shell.  I intend to refactor the function to use Popen objects at some
> point and thought perhaps escaping file names may not be entirely
> necessary.
Pass the command line to Popen as a list of strings.

More information about the Python-list mailing list