the slash & Windows paths
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Dec 19 22:29:00 EST 2011
On Mon, 19 Dec 2011 15:02:50 -0700, Juan Declet-Barreto wrote:
> <html xmlns:v="urn:schemas-microsoft-com:vml"
> xmlns:o="urn:schemas-microsoft-com:office:office"
> xmlns:w="urn:schemas-microsoft-com:office:word"
> xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
> xmlns="http://www.w3.org/TR/REC-html40"><head><meta
> http-equiv=Content-Type content="text/html; charset=us-ascii"><meta
> name=Generator content="Microsoft Word 14 (filtered medium)"><!--[if
> !mso]><style>v\:* {behavior:url(#default#VML);} o\:*
[many more lines of rubbish deleted]
Please do not post HTML code (wrongly called "rich text") emails here, as
the news group is text only.
Extracting your actual question:
> All,
>
> I have a Windows-style path that I need to modify so Python functions
> can find it. For example, the path
> "C:\Projects\Population_Pyramids\charts\test.xls" is being interpreted
> as pointing to a file called "est.xls" since the "t" is being escaped by
> the preceding slash as a tab.
It almost certainly is not. The path does not contain a backslash t, it
contains a tab. The escape occurs before the string is built.
When you write s = "abc", the quotation marks are not part of the string.
They are part of the string display: delimiters of the string, not part
of it.
Similarly, when you write s = "\t", there are no quotation marks in the
string, and no backslash, and no "t". There is only a single character,
tab. So the above path is identical to you writing:
"C:\Projects\Population_Pyramids\charts" + ord(9) + "est.xls"
To fix this, the best solution is to take advantage that Windows accepts
both forward and back slashes as directory separators:
"C:/Projects/Population_Pyramids/charts/test.xls"
Or you can build the string from components, and use os.path.join() to
build the complete string.
Or you can use raw strings as described:
> I know this is straightforward to handle using raw literals by prefixing
> the literal with an 'r', but my path is stored in a variable.
It doesn't matter whether path is stored in a variable or not. When you
build a string as a literal in source code, backslashes are always
escaped. If you read text from external data, like a file, they are never
escaped.
> My questions are: 1. How do I prevent single slashes from being escaped
> in a string variable (NOT literal)?
Backslashes are not interpreted especially in string variables. They are
only interpreted as backslashes when in source code. Compare the
difference:
>>> s = "this \n is a newline" # a string literal as source code
>>> print(s)
this
is a newline
>>> s = input("Enter text with an escape:") # data read from the user
Enter text with an escape:this \n is a newline
>>> print(s)
this \n is a newline
If you are reading the paths from a config file, they will not be
interpreted. If you are building the paths from components, like this:
folders = ['a', 'b', 'c']
os.path.join(folders)
you should get a\b\c. (I can't test this, because I am not using Windows,
so I will get something different.)
Or, best solution, take advantage that Windows accepts both back slashes
and forward slashes in path names, and always use forward slashes.
--
Steven
More information about the Python-list
mailing list