python eol while_python - "EOL while scanning single-quoted string"? (backslash in string)

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?

python

syntax-error

edited Dec 9 '11 at 3:27 asked Feb 1 '09 at 17:20 Python      could you please rename question to something more useful –

SilentGhost Feb 1 '09 at 17:27 1   Also note how Stack Overflow automatically colors your code so that the problem becomes obvious. –

ilya n. Jun 9 '09 at 18:08

|

5 Answers

5

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\

>>>

edited Feb 1 '09 at 17:27

recursive 51.9k 18 99 191 answered Feb 1 '09 at 17:25

gimel 44.8k 8 52 90      Thanks, @recursive - a slip of the fingers... –

gimel Feb 1 '09 at 17:42

|

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.

answered Feb 1 '09 at 17:26

Torsten Marek 44.5k 16 67 88 1   +1 os.path.join is the way to go. Would os.getenv('HOME') be set on Win32? I don't have an MS box to check.. –

James Brady Feb 1 '09 at 17:34      My poor little XP-in-a-VM has the "USERPROFILE" environment variable, which points to the home directory, but I don't know how official it is. –

Torsten Marek Feb 1 '09 at 17:41      os.path.join is really handy –

Hannes Ovrén Feb 1 '09 at 17:43

|

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.

answered Feb 1 '09 at 17:39

Markus Jarderot 54.4k 9 97 109      +1, works correctly on Windows 7, where it is

C:\Users\foo instead. –

Mark Tolonen Dec 9 '11 at 6:49

|

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:what i am doing wrong python

|

this question edited Mar 22 '13 at 14:34 Stephane Rolland 15.4k 23 78 125 asked Aug 24 '10 at 23:04 l--''''''---------'''''''''''' 13k 187 464 792 Are you sure it's being closed correctly a

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.

answered Feb 1 '09 at 17:53

MiniQuark 15.2k 15 85 118 1   A raw string can't end in an odd number of backslashes. Try it. –

Mark Tolonen Dec 9 '11 at 6:45      Hey, you're right. I should read the tutorial! ;-) –

MiniQuark Dec 9 '11 at 16:29

|

\" 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.

answered Dec 9 '11 at 7:09

Mark Tolonen 63.7k 4 75 132

|

Why does the backslash escape work without error here: >>> print "this is \\\ a string"this is \\ a string But when I try: >>> print "\\\" I get: SyntaxError: EOL while scanning string literal python error-handling escaping eol share |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值