为什么使用“\”在 jython 中显示错误
我正在尝试使用 Windows 的复制命令,并且我们有诸如 c:\oracle
之类的目录。
在尝试执行此类操作时,我们收到以下错误:
source_file=folder+"\"
^
SyntaxError: Lexical error at line 17, column 23. Encountered: "\r" (13), after : ""
Here 文件夹是我的 c:\oracle 路径,并且在尝试向其中添加文件时,如下所示:
source=folder+"\"+src_file
我无法这样做。关于如何解决这个问题有什么建议吗?
我尝试使用 /
但我的复制窗口在 os.command
中调用源代码时出现“语法不正确”
,这是解决该问题的唯一方法是使用 \
但这样做时出现上述错误。
请建议。感谢您的帮助
谢谢。
I am trying to use a copy command for Windows and we have directories such as c:\oracle
.
While trying to execute one such, we get the following error:
source_file=folder+"\"
^
SyntaxError: Lexical error at line 17, column 23. Encountered: "\r" (13), after : ""
Here folder is my path of c:\oracle and while trying to add file to it like:
source=folder+"\"+src_file
I am not able to do so. Any suggestion on how to solve this issue?
I tried with /
but my copy windows calling source in os.command
is getting "the syntax is incorrect"
and the only way to solve it is to use \
but I am getting the above error in doing so.
Please suggest. Thanks for your help
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短回答:
您需要:
详细回答:
问题在于
\
是转义字符。在这种特殊情况下,它所做的是转义"
,以便将其视为字符串的字符而不是字符串终止符,类似于:会出现相同的问题。
换句话说,您'尝试构造一个由
"
、其他一些文本和行尾(\r
,回车符)组成的字符串。这就是你的错误的来源:Short answer:
You need:
Long answer:
The problem with
is that
\
is the escape character. What it's doing in this particular case is escaping the"
so that it's treated as a character of the string rather than the string terminator, similar to:which would have the same problem.
In other words, you're trying to construct a string consisting of
"
, some other text and the end of line (\r
, the carriage return character). That's where your error is coming from:Paxdiablo 对于为什么 \ 不适合你是绝对正确的。但是,您也可以通过使用 os.path.normpath 来解决问题,而不是尝试自己构建正确的特定于平台的路径字符。
Paxdiablo is absolutely correct about why \ isn't working for you. However, you could also solve your problem by using
os.path.normpath
instead of trying to construct the proper platform-specific path characters yourself.在我所知道的所有编程语言中,您不能在字符串中放入引号,如下所示:
"this is a quote: "."
这样做的原因是第一个引号打开字符串,然后第二个将其关闭(!),然后第三个打开另一个字符串 - 存在以下两个问题:有两种常见的机制可以解决这个问题:加倍和转义更为常见,这意味着您可以在不需要的字符前面放置一个特殊字符(通常是
\
)。因此,"no, *this* is a quote: \"."
是一个正确的字符串,其中引号 #2 没有关闭字符串 - 和字符。\
不会出现。但是,现在您遇到了另一个问题 - 如何真正使转义字符出现在字符串中?很简单:逃离它!
“This is an escape: \\!”
是如何做到的:反斜杠 #1 是转义字符,反斜杠 #2 是转义符:它不会用通常的转义来解释语义,但作为简单的反斜杠字符。因此,你的台词应该这样说:
顺便说一句:同时支持@paxdiablo(他在我的谩骂之前就加入了)和@Nick(他有一种正确的 Pythonic 方式来做你想做的事情)
In all programming languages I know of, you can't put a quote inside a string like this:
"this is a quote: "."
The reason for this is that the first quote opens the string, the second then closes it (!), and then the third one opens another string - with the following two problems:There are two common mechanisms of solving this: doubling and escaping. Escaping is far more common, and what it means is you put a special character (usually
\
) in front of characters that you don't want to be interpreted in their usual value. Thus,"no, *this* is a quote: \"."
is a proper string, where the quote #2 is not closing the string - and the character\
does not appear.However, now you have another problem - how do you actually make the escape character appear in a string? Simple: escape it!
"This is an escape: \\!"
is how you do it: the backslash #1 is the escape character, and the backslash #2 is the escapee: it will not be interpreted with its usual escape semantics, but as a simple backslash character.Thus, your line should say this:
BTW: upvote for both @paxdiablo (who got in before my diatribe) and @Nick (who has a proper Pythonic way to do what you want to do)