简体   繁体   English

为什么在Python路径中需要4个反斜杠?

[英]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.

相关问题 为什么 python 会在路径中添加额外的反斜杠? - Why does python add additional backslashes to the path? 为什么在python中使用os.path.exists()时反斜杠不起作用,而正斜杠却起作用? - Why don't backslashes work when using os.path.exists() in python but forward slashes do work? 为什么Python字符串中的3个反斜杠等于4? - Why do 3 backslashes equal 4 in a Python string? 我不知道为什么我得到两个反斜杠(python) - I don't know why I get two backslashes (python) 为什么我需要包含 sys.path.append 来使用 Python 3.6 导入模块而我的大学不需要? - Why do I need to include sys.path.append to import a module with Python 3.6 and my colleges doesn't need? Python:为什么我指定的 output 路径中的单反斜杠在输出时更改为双反斜杠并导致 FileNotFoundError? - Python: Why are the single-backslashes in my specified output path changed to double-backslashes when outputting and result in FileNotFoundError? 如何告诉Python不要在字符串中解释反斜杠? - How do I tell Python not to interpret backslashes in strings? 为什么反斜杠出现两次? - Why do backslashes appear twice? 为什么我需要在路径转换器中使用 to_url() 方法? 姜戈 - Why do I need to_url() method in path converter? Django 从C应用程序导入Python脚本时,为什么需要在sys.path中显式添加os.getcwd()? - Why do I need to explicitly add os.getcwd() to sys.path when importing Python scripts from a C application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM