如何在 Python 中将 dos 路径拆分为其组件 [英] How to split a dos path into its components in Python

查看:29
本文介绍了如何在 Python 中将 dos 路径拆分为其组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表示 dos 路径的字符串变量,例如:

I have a string variable which represents a dos path e.g:

var = "d:\stuff\morestuff\furtherdown\THEFILE.txt"

我想把这个字符串拆分成:

I want to split this string into:

[ "d", "stuff", "morestuff", "furtherdown", "THEFILE.txt" ]

我尝试过使用 split()replace() 但它们要么只处理第一个反斜杠,要么将十六进制数字插入到字符串中.

I have tried using split() and replace() but they either only process the first backslash or they insert hex numbers into the string.

我需要以某种方式将此字符串变量转换为原始字符串,以便我可以解析它.

I need to convert this string variable into a raw string somehow so that I can parse it.

最好的方法是什么?

我还应该补充一点,var 的内容,即我试图解析的路径,实际上是命令行查询的返回值.这不是我自己生成的路径数据.它存储在一个文件中,命令行工具不会转义反斜杠.

I should also add that the contents of var i.e. the path that I'm trying to parse, is actually the return value of a command line query. It's not path data that I generate myself. Its stored in a file, and the command line tool is not going to escape the backslashes.

推荐答案

人们多次编写自己的路径摆弄函数并弄错了.空格、斜杠、反斜杠、冒号——混淆的可能性不是无穷无尽的,但无论如何都很容易犯错误.所以我坚持使用os.path,并在此基础上推荐它.

I've been bitten loads of times by people writing their own path fiddling functions and getting it wrong. Spaces, slashes, backslashes, colons -- the possibilities for confusion are not endless, but mistakes are easily made anyway. So I'm a stickler for the use of os.path, and recommend it on that basis.

(然而,美德之路并不是最容易走的,很多人发现这条路时,都想走一条滑溜溜的直奔诅咒的道路.直到有一天一切都崩溃了,他们才会意识到-- 或者更有可能是其他人 -- 必须弄清楚为什么一切都出错了,结果是有人制作了一个混合斜杠和反斜杠的文件名——有人建议答案是不要那样做";. 不要成为这些人中的任何一个.除了那些将斜杠和反斜杠混在一起的人——如果你愿意,你可以成为他们.)

(However, the path to virtue is not the one most easily taken, and many people when finding this are tempted to take a slippery path straight to damnation. They won't realise until one day everything falls to pieces, and they -- or, more likely, somebody else -- has to work out why everything has gone wrong, and it turns out somebody made a filename that mixes slashes and backslashes -- and some person suggests that the answer is "not to do that". Don't be any of these people. Except for the one who mixed up slashes and backslashes -- you could be them if you like.)

您可以像这样获取驱动器和路径+文件:

You can get the drive and path+file like this:

drive, path_and_file = os.path.splitdrive(path)

获取路径和文件:

path, file = os.path.split(path_and_file)

获取单个文件夹的名称并不是特别方便,但这种诚实的中等不适感增加了以后找到实际运行良好的东西的乐趣:

Getting the individual folder names is not especially convenient, but it is the sort of honest middling discomfort that heightens the pleasure of later finding something that actually works well:

folders = []
while 1:
    path, folder = os.path.split(path)

    if folder != "":
        folders.append(folder)
    elif path != "":
        folders.append(path)

        break

folders.reverse()

(如果路径最初是绝对路径,这会在 folders 的开头弹出一个 "\".如果不这样做,您可能会丢失一些代码想要那个.)

(This pops a "\" at the start of folders if the path was originally absolute. You could lose a bit of code if you didn't want that.)

这篇关于如何在 Python 中将 dos 路径拆分为其组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆