import os xp1 = "\Documents and Settings\" xp2 = os.getenv("USERNAME") print xp1+xp2
Gives me an error
File "1.py", line 2 xp1 = "\Documents and Settings\" ^ SyntaxError: EOL while scannning single-quoted string
Can you help me, do you see a problem?
The backslash character is interpreted as an escape. Use double backslash for window paths:
>>> xp1 = "\\Documents and Settings\\" >>> xp1 '\\Documents and Settings\\' >>> print xp1 \Documents and Settings\ >>>
Besides the problem of the black character, do not add the path by using the "+" - use os.path.join.
os.path.join
Also, create a path to the user's home directory, which is likely to fail in new versions of Windows. For this, pywin32 has API functions.
os.path.expanduser, . .
os.path.expanduser
>>> 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" . "~" .
~user
~
Python, , escape- ( xp1 =... , ).
, python, .
, . r :
xp1 = r"\Documents and Settings\"
, os.path, "/" "\" O.S. . :
import os.path xp1 = os.path.join("data","cities","geo.txt")
"//geo.txt" Linux "data\cities\geo.txt" Windows.
\" " , . , raw r"\" .
\"
r"\"
( ):
'r' 'R', , , , . , r "\n" : "n". , ; , r "\" " , : ; r "\" ( ) In particular, a raw string cannot end with a single backslash (since a backslash avoids the next quote character). Note also that a single backslash followed by a newline character is interpreted as the two characters as part of a string, and not as a continuation of a line.
@MizardX's answer provides the correct way to code what you do, independently.
Source: https://habr.com/ru/post/1702551/More articles:Modeling C - c ApplicationsUrlRewriting для кэширования выходных данных Global.asax и SQL - c#More natural boost :: bind alternative? - c ++How to create a random simulation? - randomTagging XMP and Python tags - pythonhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1702552/drop-down-menu-needs-to-display-over-everything-but-it-is-underneath-a-certain-div&usg=ALkJrhh_Uz9wUFYOFV9u0-c_jVxhL-oe5QRuby way to create n-ary wood - ruby | fooobar.comDevelopment in a hostile environment - development-environmentPHP Accelerated Download Method? - phpHow do you map a component that is also a primary key in NHibernate hbm xml (or in a map of the fluent-nhibernate class)? - c #All Articles