如何获得路径的正确大小写? [英] How can I get the proper capitalization for a path?
问题描述
假设我有一个代表目录的类(当然是简化示例):
Let's say I have a class which represents a directory (simplified example of course):
import os
class Dir:
def __init__(self, path):
self.path = os.path.normcase(path)
为了使事情更容易在内部实现,我在呼叫path参数上的> os.path.normcase
保存到属性之前.这很好用,但是会小写路径:
To make things easier to implement internally, I am calling os.path.normcase
on the path
argument before I save it into an attribute. This works great, but it lowercases the path:
>>> import os
>>> os.path.normcase(r'C:\Python34\Lib')
'c:\\python34\\lib'
>>>
我想要一种将路径转换为正确的大写形式的C:\Python34\Lib
的方法.我计划在__repr__
方法中执行此操作,以便获得不错的输出,例如:
I would like a way to turn the path back into its properly capitalized form of C:\Python34\Lib
. I plan to do this inside the __repr__
method so that I can get nice outputs such as:
>>> my_dir
Dir(r'C:\Python34\Lib')
>>>
当我在交互式口译员中时.标准库中有类似的东西吗?
when I am in the interactive interpreter. Is there anything like this in the standard library?
注意:我不是指用户作为path
参数提供的字符串.如果用户这样做:
Note: I am not referring to the string that the user supplied as the path
argument. If a user does:
my_dir = Dir('c:\PYTHON34\lib')
我仍然希望在解释器中打印Dir('C:\Python34\Lib')
,因为这是正确的大写形式.基本上,我希望输出的路径与文件资源管理器中的路径相同.
I still want Dir('C:\Python34\Lib')
to be printed in the interpreter because that is the proper capitalization. Basically, I want the outputed paths to be the same as they are in the file explorer.
推荐答案
更新:
对于使用新版Python的用户,新的 pathlib
模块以 pathlib.Path.resolve
的形式具有此功能:
For those using the newer versions of Python, the new pathlib
module possesses this functionality in the form of pathlib.Path.resolve
:
>>> from pathlib import Path
>>> Path(r'c:\python34\lib').resolve()
WindowsPath('C:/Python34/Lib')
>>> str(Path(r'c:\python34\lib').resolve())
'C:\\Python34\\Lib'
>>>
因此,您可以将用户提供的路径存储为Path
对象:
So, you could store the user-supplied path as a Path
object:
from pathlib import Path
class Dir:
def __init__(self, path):
self.path = Path(path)
,然后像这样实现__repr__
方法:
and then implement the __repr__
method like so:
def __repr__(self):
return "Dir('{}')".format(self.path.resolve())
另外,由于Path
对象直接支持不区分大小写的比较,因此我们不再需要os.path.normcase
函数.
As an added bonus, we no longer need the os.path.normcase
function since Path
objects support case-insensitive comparisons directly.
pathlib
的一个缺点是,它仅在Python 3.4(当前最新版本)中可用.因此,使用早期版本的用户将需要向后移植到其版本,或使用如下所示的os.path._getfinalpathname
函数.
One downside to pathlib
though is that it is only available in Python 3.4 (the currently newest version). So, those using earlier versions will need to either get a backport to their version or use the os.path._getfinalpathname
function as demonstrated below.
在我浏览标准库时,我在名为_getfinalpathname
的os.path
模块中遇到了一个未记录的函数:
While I was digging through the standard library, I came across an undocumented function in the os.path
module named _getfinalpathname
:
>>> import os
>>> os.path._getfinalpathname(r'c:\python34\lib')
'\\\\?\\C:\\Python34\\Lib'
>>>
使用 str.lstrip
,我可以获得输出我需要:
Using str.lstrip
, I can get the output I need:
>>> os.path._getfinalpathname(r'c:\python34\lib').lstrip(r'\?')
'C:\\Python34\\Lib'
>>>
此方法的唯一缺点是该函数没有文档说明,并且有些隐藏.但这现在适合我的需求(当然,如果您知道一种方法,我很想听听更好的方法:)
The only downside to this approach is that the function is undocumented and somewhat hidden. But it suits my needs for now (of course, I'd love to hear a better approach if you know of one :)
这篇关于如何获得路径的正确大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!