Thread: From \ to \\

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    5
    Rep Power
    0

    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.

    Code:
    import _winreg as wreg
    import os.path
    class WindowsRegistry:
    
        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
        key.Close()
    
        print a
        b = a + 'Interface'
        print b
    The attachement is the regkey that the program looks for, for those who don't want to write their own.

    Peace out,

    -Kingdud
    Attached Files
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    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:

    Code:
    b = a + 'Interface\\'
    The first \ escapes the second, so it is treated as a backslash rather than a command character.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    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:
    Code:
    keypath = os.path.join('SOFTWARE', 'Blizzard', 'Entertainment', 'World of Warcraft')
    b = os.path.join(keypath, "Interfaces")
    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:
    Code:
        keypath = r"SOFTWARE\Blizzard Entertainment\World of Warcraft\\"
    --OH.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.

    Code:
    >>> 'C:\Python23\python.exe'
    'C:\\Python23\\python.exe'
    >>> r'C:\Python23\python.exe'
    'C:\\Python23\\python.exe'
    >>> path = 'C:\Python23\python.exe'
    >>> path
    'C:\\Python23\\python.exe'
    >>> 
    >>> 'C:\Python23\python.exe\'
    SyntaxError: EOL while scanning single-quoted string
    >>>
    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    In Python 2.3+ backslashes are automatically escaped for you
    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:
    Code:
    >>> x = "c:\python\lib\nasty.py"
    >>> x
    'c:\\python\\lib\nasty.py'
    >>> print x
    c:\python\lib
    asty.py
    Moral: Always use escaped backslashes or raw strings, even whn you don't have to.

    --OH.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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,

    Mark .
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo