[英]Why do I need 4 backslashes in a Python path?
When I'm using Python 3 to launch a program via subprocess.call()
, why do I need 4 backslashes in paths? 当我使用Python 3通过
subprocess.call()
启动程序时,为什么在路径中需要4个反斜杠?
This is my code: 这是我的代码:
cmd = 'C:\\\\Windows\\\\System32\\\\cmd.exe'
cmd = shlex.split(cmd)
subprocess.call(cmd)
When I examine the command line of the launched cmd.exe
instance with Task Manager, it shows the path correctly with only one backslash separating each path. 当我使用任务管理器检查启动的
cmd.exe
实例的命令行时,它正确显示了路径,并且每个路径之间只有一个反斜杠。
Because of this, I need this on Windows to make the paths work: 因此,我需要在Windows上使路径起作用:
if platform.platform().startswith('Windows'):
cmd = cmd.replace(os.sep, os.sep + os.sep)
is there a more elegant solution? 有没有更优雅的解决方案?
When you are creating the string, you need to double each backslash for escaping, and then when the string is passed to your shell, you need to double each backslash again. 创建字符串时,需要将每个反斜杠加倍以进行转义,然后将字符串传递到外壳时,需要再次将每个反斜杠加倍。 You can cute the backslashes in half by using a raw string:
您可以使用原始字符串将反斜杠分成两半:
cmd = r'C:\\Windows\\System32\\cmd.exe'
Part of the problem is that you're using shlex
, which implements escaping rules used by Unix-ish shells. 问题的一部分是您正在使用
shlex
,它实现了Unix类shell使用的转义规则。 But you're running on Windows, whose command shells use different rules. 但是您正在Windows上运行 ,其命令外壳使用不同的规则。 That accounts for one level of needing to double backslashes (ie, to worm around something
shlex
does that you didn't need to begin with). 这说明了需要加倍反斜杠的一个层次(即,绕过
shlex
不需要的东西)。
That you're using a regular string instead of a raw string ( r"..."
) accounts for the other level of needing to double backslashes, and 2*2 = 4. QED ;-) 您使用的是常规字符串而不是原始字符串(
r"..."
),这说明了需要将反斜杠加倍的另一种情况,即2 * 2 =4。QED ;-)
This works fine on Windows: 在Windows上可以正常使用:
cmd = subprocess.call(r"C:\Windows\System32\cmd.exe")
By the way, read the docs for subprocess.Popen()
carefully: the Windows CreateProcess()
API call requires a string for an argument. 顺便说一句,请仔细阅读
subprocess.Popen()
的文档:Windows CreateProcess()
API调用需要一个字符串作为参数。 When you pass a sequence instead, Python tries to turn that sequence into a string, via rules explained in the docs. 当您传递序列时,Python会尝试按照文档中说明的规则将该序列转换为字符串。 When feasible, it's better - on Windows - to pass the string you want directly.
在可行的情况下,最好在Windows上直接传递您想要的字符串。
\\
has special meaning - you're using it as part of an escape sequence. \\
具有特殊含义-您将其用作转义序列的一部分。 Double up the backslashes, and you have a literal backslash \\
. 将反斜杠加倍,您将得到一个文字反斜杠
\\
。
The caveat is that, with only one pair of escaped backslashes, you still have only one literal backslash. 需要注意的是,只有一对转义的反斜杠, 您仍然只有一个文字反斜杠。 You need to escape that backslash, too.
你需要逃离反斜杠 ,太。
Alternatively, why not just use os.sep
instead? 另外,为什么不只使用
os.sep
呢? You'll be able to ensure your code is more portable (since it'll use the system-specific separator), and you won't have to deal [directly] with escaping backslashes. 您将能够确保您的代码更具可移植性(因为它将使用系统特定的分隔符),并且您不必[直接]转义反斜杠。
As John points out 4 slashes isn't necessary when accessing files locally. 正如John指出的那样,在本地访问文件时,不需要4个斜杠。 One place where 4 slashes is necessary is when connecting to (generally windows) servers over SMB or CIFS.
通过SMB或CIFS连接到(通常是Windows)服务器时,需要使用4个斜杠的地方。
Normally you would just use \\servername\\share\\ 通常,您只需要使用\\ servername \\ share \\
But each one of those slashes needs to be escaped. 但是这些斜线中的每一个都需要逃脱。 So thus the 4 slashes before servernames.
因此,服务器名之前的4个斜杠。
you could also use subprocess.call() 您也可以使用subprocess.call()
import subprocess as sp
sp.call(['c:\\program files\\<path>'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.