“EOL while scanning single-quoted string”? (backslash in string)

前端 未结 5 1684
心在旅途
心在旅途 2020-12-06 08:05
import os
xp1 = \"\\Documents and Settings\\\"
xp2 = os.getenv(\"USERNAME\")
print xp1+xp2

Gives me error

 File \"1.py\", line 2 
x         


        
相关标签:
5条回答
  • 2020-12-06 08:28

    The backslash character is interpreted as an escape. Use double backslashes for windows paths:

    >>> xp1 = "\\Documents and Settings\\"
    >>> xp1
    '\\Documents and Settings\\'
    >>> print xp1
    \Documents and Settings\
    >>> 
    
    0 讨论(0)
  • 2020-12-06 08:36

    You can use the os.path.expanduser function to get the path to a users home-directory. It doesn't even have to be an existing user.

    >>> import os.path
    >>> os.path.expanduser('~foo')
    'C:\\Documents and Settings\\foo'
    >>> print os.path.expanduser('~foo')
    C:\Documents and Settings\foo
    >>> print os.path.expanduser('~')
    C:\Documents and Settings\MizardX
    

    "~user" is expanded to the path to user's home directory. Just a single "~" gets expanded to the current users home directory.

    0 讨论(0)
  • 2020-12-06 08:40

    Python, as many other languages, uses the backslash as an escape character (the double-quotes at the end of your xp1=... line are therefore considered as part of the string, not as the delimiter of the string).

    This is actually pretty basic stuff, so I strongly recommend you read the python tutorial before going any further.

    You might be interested in raw strings, which do not escape backslashes. Just add r just before the string:

    xp1 = r"\Documents and Settings\"
    

    Moreover, when manipulating file paths, you should use the os.path module, which will use "/" or "\" depending on the O.S. on which the program is run. For example:

    import os.path
    xp1 = os.path.join("data","cities","geo.txt")
    

    will produce "data/cities/geo.txt" on Linux and "data\cities\geo.txt" on Windows.

    0 讨论(0)
  • 2020-12-06 08:50

    \" is interpreted as "insert a double-quote into the string, so you are missing a terminating quote for the string literal. Note that a raw string r"\" can't help either.

    Quote from the documentation (bold is mine):

    When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.

    The answer @MizardX gave is the right way to code what you are doing, regardless.

    0 讨论(0)
  • 2020-12-06 08:51

    Additionally to the blackslash problem, don't join paths with the "+" operator -- use os.path.join instead.

    Also, construct the path to a user's home directory like that is likely to fail on new versions of Windows. There are API functions for that in pywin32.

    0 讨论(0)
提交回复
热议问题