Reputation:
import os
xp1 = "\Documents and Settings\"
xp2 = os.getenv("USERNAME")
print xp1+xp2
Gives me error
File "1.py", line 2
xp1 = "\Documents and Settings\"
^
SyntaxError: EOL while scannning single-quoted string
Can you help me please, do you see the problem?
Upvotes: 7
Views: 43790
Reputation: 6618
In Windows
for file in glob.glob('C:\Users\7000020370\3D Objects\audio\data\*\*'):
print(file.split("\\")[0])
This will throw error , but the below one will not
for file in glob.glob(r'C:\Users\7000020370\3D Objects\audio\data\*\*'):
print(file.split("\\")[0])
Upvotes: 0
Reputation: 368
I had the same error, I solved it reading the comment of @Mark Tolonen
This is a wrong path string:
xp1 = "\Documents and Settings\"
This is a right path string, without the last back slash:
xp1 = "\Documents and Settings"
Upvotes: 1
Reputation: 177554
\"
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.
Upvotes: 5
Reputation: 48436
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.
Upvotes: 6
Reputation: 89171
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.
Upvotes: 8
Reputation: 86354
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\
>>>
Upvotes: 23
Reputation: 86492
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.
Upvotes: 11