[Tutor] Raw string form variable

Steven D'Aprano steve at pearwood.info
Sun May 5 11:03:07 CEST 2013


On 05/05/13 18:00, Ajin Abraham wrote:
> Please refer this paste: http://bpaste.net/show/vsTXLEjwTLrWjjnfmmKn/
> and suggest me the possible solutions.

There is no need for a paste bin for this. In six months time, when other people are searching the mail archives looking for answers, the paste bin will be expired and gone and your post will be pointless. We are here to help everybody, including people who come in the future and read over existing posts.

Your question says:


[quote]

i am executing these in Python 2.7 interpreter

>>>import os
>>> os.path.join(r'C:\win\apple.exe')
#will returns me = 'C:\\win\\apple.exe'

>>> path='c:\win\apple.exe'
#here for example i assign the path. but in my case value is assigned to my variable after a file read operation.

>>> path
'c:\\win\x07pple.exe'
# i know '\a' is encountered here.
#but i need it as 'C:\\win\\apple.exe'

How can i convert the content of a variable to a raw string.

help me out

this is a solution but it didn't worked out: http://code.activestate.com/recipes/65211/
[end quote]


which is short enough that a paste bin is unnecessary.


Firstly, you say:

os.path.join(r'C:\win\apple.exe')


but that's a waste of time, because you're not joining anything. You only have one path. Normally you would join two or more path components:

py> os.path.join("a/b/c", "d/e", "f")
'a/b/c/d/e/f'


os.path.join will work with a single path component, but it doesn't really do anything so there is no point.

In this case, you will see that raw strings are not a different kind of string, they are just a different syntax for creating string literals. So we have:

py> a = r'C:\win\apple.exe'  # Use raw string syntax
py> b = 'C:\\win\\apple.exe'  # Use normal string syntax, with escaped backslashes
py> print a, b, a == b
C:\win\apple.exe C:\win\apple.exe True



When you do this:

path = 'c:\win\apple.exe'


you are not using a raw string, so backslashes have special meaning. Backslash-w has no special meaning and is left as given, but backslash-a means the ASCII BEL character.

If you read path from a file, then backslashes are just backslashes, and nothing special happens. It is only when you type a literal string in Python source code that backslashes are special.

For example:

py> literal = '\a'  # String literal, backslashes are special
py> variable = chr(92) + 'a'  # backslash-a
py> (literal, variable, literal == variable)
('\x07', '\\a', False)


The thing that you may not have understood is that backslash escapes are not part of the string, they are part of the syntax, like the quote delimiters. When you type:

s = "abc"

the string s has THREE characters, a b c, and not five characters quote a b c quote. The quotes are only there to tell Python that you want it to create a string. The same with backslashes: they are there only to tell Python to add a special character:

s = "abc\n"

means to create the FOUR character string, a b c newline, not seven characters quote a b c backslash n quote.

This "magic" only happens when Python reads string constants in source code, not whenever it sees a string. So if you have a config file containing the string "c:\win\apple.exe", and you read from the file and assign to the variable path, then path will contain:

c colon backslash w i n backslash a p p l e dot e x e


which is exactly what you need. You DON'T need:

c colon backslash backslash w i n backslash backslash a p p l e dot e x e


although I suppose Windows may be able to cope with the unneeded, extra backslashes.


Another alternative is to just use forward slashes, since Windows can cope with them fine:

path = "c:/win/apple.exe"



-- 
Steven


More information about the Tutor mailing list