On 12/19/2011 05:02 PM, Juan Declet-Barreto wrote:
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.

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.

My questions are: 1. How do I prevent single slashes from being escaped in a 
string variable (NOT literal)?
                                       2. Is it possible to do a string.replace 
to substitute all instances of '\' for '\\'? I tried this but it did not give 
me the desired result.

So far you haven't told us how you got this weird pattern into your variable. For example, if it was typed in by the user, in the form:

    mypath = raw_input("what's yourpath?")

then the question is nonsensical. There's no escaping needed, as it has never been a literal string.

if it came from a text file, there's no escaping needed.

So give us a real scenario, and we'll probably be able to figure out where you're going wrong.



--

DaveA

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to