删除路径中的第一个文件夹 [英] Removing the first folder in a path

查看:87
本文介绍了删除路径中的第一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条看起来像

/First/Second/Third/Fourth/Fifth

,我想从中删除First,从而获得

and I would like to remove the First from it, thus obtaining

Second/Third/Fourth/Fifth

我唯一想出的主意是递归使用os.path.split,但这似乎不是最佳选择.有更好的解决方案吗?

The only idea I could come up with is to use recursively os.path.split but this does not seem optimal. Is there a better solution?

推荐答案

os.path模块中实际上没有任何东西可以执行此操作.经常有人建议创建一个splitall函数,该函数返回所有组件的列表(或迭代器),但从未获得足够的吸引力.

There really is nothing in the os.path module to do this. Every so often, someone suggests creating a splitall function that returns a list (or iterator) of all of the components, but it never gained enough traction.

部分原因是,每当有人建议向os.path添加新功能时,它都会重新点燃人们对库的一般设计的长期不满,从而导致有人提出了新的,更类似于OO的API对于不推荐使用的操作系统,笨拙的API.在3.4中,这终于发生了,使用 pathlib .而且它已经具有os.path中没有的功能.所以:

Partly this is because every time anyone ever suggested adding new functionality to os.path, it re-ignited the long-standing dissatisfaction with the general design of the library, leading to someone proposing a new, more OO-like, API for paths to deprecated the os, clunky API. In 3.4, that finally happened, with pathlib. And it's already got functionality that wasn't in os.path. So:

>>> import pathlib
>>> p = pathlib.Path('/First/Second/Third/Fourth/Fifth')
>>> p.parts[2:]
('Second', 'Third', 'Fourth', 'Fifth')
>>> pathlib.Path(*p.parts[2:])
PosixPath('Second/Third/Fourth/Fifth')

或者……您确定要删除第一个组件,而不是这样做吗?

Or… are you sure you really want to remove the first component, rather than do this?

>>> p.relative_to(*p.parts[:2])
PosixPath('Second/Third/Fourth/Fifth')

如果您需要在2.6-2.7或3.2-3.3中执行此操作,则后向移植> .

If you need to do this in 2.6-2.7 or 3.2-3.3, there's a backport of pathlib.

当然,只要小心地标准化路径并使用os.path.sep,并确保使用非绝对路径或带驱动器号的系统来处理复杂的细节,就可以使用字符串操作,和...

Of course, you can use string manipulation, as long as you're careful to normalize the path and use os.path.sep, and to make sure you handle the fiddly details with non-absolute paths or with systems with drive letters, and…

或者您也可以只包装递归os.path.split.打包后,非最佳"到底是什么?它可能会慢一些,但是我们在这里谈论的是纳秒级,比甚至在文件上调用stat都快了多个数量级.如果您的文件系统深达1000个目录,那么它将遇到递归深度问题,但是您见过吗? (如果是这样,您总是可以把它变成一个循环……)将它包装起来并编写好的单元测试需要花费几分钟,但是您只需要执行一次就可以了,而不必再担心了.因此,说实话,如果您不想使用pathlib,那就是我要做的.

Or you can just wrap up your recursive os.path.split. What exactly is "non-optimal" about it, once you wrap it up? It may be a bit slower, but we're talking nanoseconds here, many orders of magnitude faster than even calling stat on a file. It will have recursion-depth problems if you have a filesystem that's 1000 directories deep, but have you ever seen one? (If so, you can always turn it into a loop…) It takes a few minutes to wrap it up and write good unit tests, but that's something you just do once and never worry about again. So, honestly, if you don't want to use pathlib, that's what I'd do.

这篇关于删除路径中的第一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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