escaping characters in filenames
davea at ieee.org
Wed Jul 29 14:33:25 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.
> As an aside, the script is using subprocess.call() 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
There are dozens of meanings for escaping characters in strings.
Without some context, we're wasting our time.
For example, if the filename is to be interpreted as part of a URL, then
spaces are escaped by using %20. Exactly who is going to be using this
string you think you have to modify? I don't know of any environment
which expects spaces to be escaped with backslashes.
Be very specific. For example, if a Windows application is parsing its
own command line, you need to know what that particular application is
expecting -- Windows passes the entire command line as a single string.
But of course you may be invoking that application using
subprocess.Popen(), in which case some transformations happen to your
arguments before the single string is built. Then some more
transformations may happen in the shell. Then some more in the C
runtime library of the new process (if it happens to be in C, and if it
happens to use those libraries).
I'm probably not the one with the answer. But until you narrow down
your case, you probably won't attract the attention of whichever person
has the particular combination of experience that you're hoping for.
More information about the Python-list