Python:获取 Windows 中存储的文件名大小写?

发布于 2024-08-18 22:15:03 字数 172 浏览 7 评论 0原文

尽管 Windows 不区分大小写,但它确实保留文件名的大小写。在Python中,有什么方法可以获取存储在文件系统上的带有大小写的文件名吗?

例如,在Python程序中,我有filename =“texas.txt”,但想知道它实际上存储在文件系统上“TEXAS.txt”,即使这对于各种文件操作来说是无关紧要的。

Though Windows is case insensitive, it does preserve case in filenames. In Python, is there any way to get a filename with case as it is stored on the file system?

E.g., in a Python program I have filename = "texas.txt", but want to know that it's actually stored "TEXAS.txt" on the file system, even if this is inconsequential for various file operations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

岁月蹉跎了容颜 2024-08-25 22:15:03

这是最简单的方法:

>>> import win32api
>>> win32api.GetLongPathName(win32api.GetShortPathName('texas.txt')))
'TEXAS.txt'

Here's the simplest way to do it:

>>> import win32api
>>> win32api.GetLongPathName(win32api.GetShortPathName('texas.txt')))
'TEXAS.txt'
倾听心声的旋律 2024-08-25 22:15:03

我在使用上面的 win32api 解决方案时遇到了特殊字符问题。对于 unicode 文件名,您需要使用:

win32api.GetLongPathNameW(win32api.GetShortPathName(path))

I had problems with special characters with the win32api solution above. For unicode filenames you need to use:

win32api.GetLongPathNameW(win32api.GetShortPathName(path))
烈酒灼喉 2024-08-25 22:15:03

这个仅是标准库,可以转换所有路径部分(驱动器号除外):

def casedpath(path):
    r = glob.glob(re.sub(r'([^:/\\])(?=[/\\]|$)|\[', r'[\g<0>]', path))
    return r and r[0] or path

此外,这个还可以处理 UNC 路径:

def casedpath_unc(path):
    unc, p = os.path.splitunc(path)
    r = glob.glob(unc + re.sub(r'([^:/\\])(?=[/\\]|$)|\[', r'[\g<0>]', p))
    return r and r[0] or path

注意:它比依赖于文件系统要慢一些> Win API“GetShortPathName”方法,但适用于平台和平台独立于文件系统,并且在 Windows 卷上关闭短文件名生成时也是如此(fsutil.exe 8dot3name query C:)。至少推荐后者对于性能关键的文件系统,当不再有 16 位应用程序依赖时:

fsutil.exe behavior set disable8dot3 1

This one is standard library only and converts all path parts (except drive letter):

def casedpath(path):
    r = glob.glob(re.sub(r'([^:/\\])(?=[/\\]|$)|\[', r'[\g<0>]', path))
    return r and r[0] or path

And this one handles UNC paths in addition:

def casedpath_unc(path):
    unc, p = os.path.splitunc(path)
    r = glob.glob(unc + re.sub(r'([^:/\\])(?=[/\\]|$)|\[', r'[\g<0>]', p))
    return r and r[0] or path

Note: It is somewhat slower than the file system dependent Win API "GetShortPathName" method, but works platform & file system independent and also when short filename generation is switched off on Windows volumes (fsutil.exe 8dot3name query C:). The latter is recommended at least for performance critical file systems when no 16bit apps rely anymore on that:

fsutil.exe behavior set disable8dot3 1
挽你眉间 2024-08-25 22:15:03
>>> import os
>>> os.listdir("./")
['FiLeNaMe.txt']

这能回答你的问题吗?

>>> import os
>>> os.listdir("./")
['FiLeNaMe.txt']

Does this answer your question?

平定天下 2024-08-25 22:15:03

如果你想递归目录

import os
path=os.path.join("c:\\","path")
for r,d,f in os.walk(path):
    for file in f:
        if file.lower() == "texas.txt":
              print "Found: ",os.path.join( r , file )

and if you want to recurse directories

import os
path=os.path.join("c:\\","path")
for r,d,f in os.walk(path):
    for file in f:
        if file.lower() == "texas.txt":
              print "Found: ",os.path.join( r , file )
十年九夏 2024-08-25 22:15:03

您可以使用:

import os
a = os.listdir('mydirpath')
b = [f.lower() for f in a]
try:
    i = b.index('texas.txt')
    print a[i]
except ValueError:
    print('File not found in this directory')

这当然假设您的搜索字符串 'texas.txt' 是小写的。如果不是,您必须先将其转换为小写。

You could use:

import os
a = os.listdir('mydirpath')
b = [f.lower() for f in a]
try:
    i = b.index('texas.txt')
    print a[i]
except ValueError:
    print('File not found in this directory')

This of course assumes that your search string 'texas.txt' is in lowercase. If it isn't you'll have to convert it to lowercase first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文