February 21st, 2005, 03:20 PM
From \ to \\
Ok, so I am slowly getting my program to working status. Anywho, I am trying to get it to read a registry key (done), split the tuple output into a string and an unused variable (done), then convert the string (which is a folder location) into a python-useable path; IE: One with \\ or / in the path instead of \
Ultimatly the path I get from that will be used for an os.path.walk operation, I just can't figure out how to append the extra / on the string. Only windows based machines will be using this program.
Here is the code I have to read and interpret the key/tuple output.
The attachement is the regkey that the program looks for, for those who don't want to write their own.
import _winreg as wreg
keypath = "SOFTWARE\\Blizzard Entertainment\\World of Warcraft\\"
key = wreg.OpenKey(wreg.HKEY_LOCAL_MACHINE, keypath)
a, gayness = wreg.QueryValueEx(key,'InstallPath') #breaks the tuple into two variables
b = a + 'Interface'
February 21st, 2005, 07:34 PM
os.path.walk doesn't need a \ at the end of a path to work.
As you mention in the thread title, you can add a \ with:
The first \ escapes the second, so it is treated as a backslash rather than a command character.
b = a + 'Interface\\'
February 25th, 2005, 10:32 PM
Even though you're windows only, you're better off using os.path.join since there's less work needed to ensure that you get the backslashes correct:
If you _really_ feel the need to use a lot of backslashes, then use raw strings, but be aware that you'll still need to use a double bslash at the end since \" is still treated as an escape:
keypath = os.path.join('SOFTWARE', 'Blizzard', 'Entertainment', 'World of Warcraft')
b = os.path.join(keypath, "Interfaces")
keypath = r"SOFTWARE\Blizzard Entertainment\World of Warcraft\\"
February 26th, 2005, 02:16 PM
In Python 2.3+ backslashes are automatically escaped for you, this works fine just remember to either escape or delete the backslash if it is the last character in the string.
>>> path = 'C:\Python23\python.exe'
SyntaxError: EOL while scanning single-quoted string
February 27th, 2005, 07:04 PM
Not exactly true, and a very bad habit to get into. It's a python wart that any non-escape sequence is left untouched rather than raising an exception. If the character after the backslash is an "n" or a "t", for instance, you'll have problems:
Moral: Always use escaped backslashes or raw strings, even whn you don't have to.
>>> x = "c:\python\lib\nasty.py"
>>> print x
February 27th, 2005, 07:56 PM
That's a very good point! One that I hadn't really considered, probably because I rarely use the back-slash in filenames . Even when I am on Windows I tend to stick to the familiar forward-slash .
Thanks for the info Hydroxide,