简体   繁体   English

解析python中变量的目录路径

[英]Parse directory path to a variable in python

I want to parse to var and set what i parse to a variable I want to parse C:\\\\level1\\\\level2\\\\level3\\\\level4\\\\level5\\\\level6\\\\level7\\\\level8 and make it into level7\\level8` currently i am able to get level7 only我想解析为 var 并将我解析的内容设置为我想解析的变量C:\\\\level1\\\\level2\\\\level3\\\\level4\\\\level5\\\\level6\\\\level7\\\\level8 and make it into level7\\ level8` 目前我只能达到 level7

var = "C:\\level1\\level2\\level3\\level4\\level5\\level6\\level7\\level8"

split_path = os.path.split(os.path.split(var)[0])

print split_path

output below下面的输出

('C:\\level1\\level2\\level3\\level4\\level5\\level6', 'level7')

The reason you only get 'level7' is because 'level8' is in你只得到 'level7' 的原因是因为 'level8' 在

os.path.split(var)[1]

This should demonstrate it clearly:这应该清楚地表明:

var = "C:\\level1\\level2\\level3\\level4\\level5\\level6\\level7\\level8"
split_path = os.path.split(var)
level8 = split_path[1]
split_path = os.path.split(split_path[0])
level7 = split_path[1]
my_split_path = (split_path[0], os.path.join(level7, level8)

Here's the whole thing in one line:这是一行的全部内容:

my_split_path = (os.path.split(os.path.split(var)[0])[0], os.path.split(os.path.join(os.path.split(var)[0][1], os.path.split(var)[1])))

I'd recommend using multiple lines for clarity though.为清楚起见,我建议使用多行。

As far as the double backslashes, Python string literals treat the first backslash as an escape character.至于双反斜杠,Python 字符串文字将第一个反斜杠视为转义字符。 So having two of them is effectively having one of them for all uses.因此,拥有其中的两个实际上是拥有其中一个可用于所有用途。 In the interpreter var will output the string as you have it above, but print(var) will print single backslashes.在解释器中, var将像上面一样输出字符串,但print(var)将打印单个反斜杠。 This question has been answered in detail here: Why can't Python's raw string literals end with a single backslash?这里已经详细回答了这个问题: 为什么 Python 的原始字符串文字不能以单个反斜杠结尾?

>>> var = "C:\\level1\\level2\\level3\\level4\\level5\\level6\\level7\\level8"
>>> var.split('\\', 7)
['C:', 'level1', 'level2', 'level3', 'level4', 'level5', 'level6', 'level7\\level8']
>>> var.split('\\', 7)[-1]
'level7\\level8'

I don't get the same output as you with your code.我的代码没有得到与您相同的输出。 It could be because I'm on OSX and that isn't recognized as a file path.这可能是因为我在 OSX 上并且未被识别为文件路径。 Regardless, this solution is a bit simpler and gives you the bit you want.无论如何,这个解决方案更简单一些,并为您提供您想要的东西。

The second parameter tells split the max number of times to split counting from left, so:第二个参数告诉 split 从左边开始拆分计数的最大次数,因此:

>>> var.split('\\', 1)
['C:', 'level1\\level2\\level3\\level4\\level5\\level6\\level7\\level8']
>>> var.split('\\', 2)
['C:', 'level1', 'level2\\level3\\level4\\level5\\level6\\level7\\level8']
# and so on...

You don't need to do anything to remove the extra '\\' , it's there to escape the one you want.你不需要做任何事情来删除额外的'\\' ,它是为了逃避你想要的。 You can see this by using print() :您可以使用print()看到这一点:

>>> print(var.split('\\', 7)[-1])
level7\level8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM