有没有一种简单的方法在Python中检查文件系统是否不区分大小写?我特别考虑像HFS+(OSX)和NTFS(Windows)这样的文件系统,在这些文件系统上,您可以访问与foo、Foo或FOO相同的文件,即使文件名大小写不同也可以。
有没有一种简单的方法在Python中检查文件系统是否不区分大小写?我特别考虑像HFS+(OSX)和NTFS(Windows)这样的文件系统,在这些文件系统上,您可以访问与foo、Foo或FOO相同的文件,即使文件名大小写不同也可以。
import os
import tempfile
# By default mkstemp() creates a file with
# a name that begins with 'tmp' (lowercase)
tmphandle, tmppath = tempfile.mkstemp()
if os.path.exists(tmppath.upper()):
# Case insensitive.
else:
# Case sensitive.
除非显式处理关闭和删除,否则Amber提供的答案将留下临时文件碎片。为了避免这种情况,我使用:
import os
import tempfile
def is_fs_case_sensitive():
#
# Force case with the prefix
#
with tempfile.NamedTemporaryFile(prefix='TmP') as tmp_file:
return(not os.path.exists(tmp_file.name.lower()))
尽管我的使用情况通常会测试这个结果不止一次,因此我将结果存储起来以避免多次触碰文件系统。
def is_fs_case_sensitive():
if not hasattr(is_fs_case_sensitive, 'case_sensitive'):
with tempfile.NamedTemporaryFile(prefix='TmP') as tmp_file:
setattr(is_fs_case_sensitive,
'case_sensitive',
not os.path.exists(tmp_file.name.lower()))
return(is_fs_case_sensitive.case_sensitive)
如果只调用一次,则速度略慢,而在其他情况下速度显著更快。
def is_fs_case_sensitive(path):
#
# Force case with the prefix
#
with tempfile.NamedTemporaryFile(prefix='TmP',dir=path, delete=True) as tmp_file:
return(not os.path.exists(tmp_file.name.lower()))
对@Shrikant答案的变化,适用于模块内部(即不在REPL中),即使您的用户没有主目录:
import os.path
is_fs_case_insensitive = os.path.exists(__file__.upper()) and os.path.exists(__file__.lower())
print(f"{is_fs_case_insensitive=}")
is_fs_case_insensitive=True
还有 Linux 方面的事情:
(ssha)vagrant ~$python3.8 test.py
is_fs_case_insensitive=False
(ssha)vagrant ~$lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04 LTS
Release: 20.04
Codename: focal
顺便提一下,我通过以下方式检查了 pathlib
, os
, os.path
的内容:
[k for k in vars(pathlib).keys() if "case" in k.lower()]
然而没有发现类似的内容,虽然它确实有一个 pathlib.supports_symlinks
,但是没有关于大小写敏感度的信息。
并且以下也将在 REPL 中起作用:
is_fs_case_insensitive = os.path.exists(os.path.__file__.upper()) and os.path.exists(os.path.__file__.lower())
/some/dir/foo
和 /SOME/DIR/FOO
。 - FMc我认为这个问题有一个更简单(也可能更快)的解决方案。在我测试的地方,以下方法似乎有效:
import os.path
home = os.path.expanduser('~')
is_fs_case_insensitive = os.path.exists(home.upper()) and os.path.exists(home.lower())
从Amber的回答开始,我想出了这段代码。我不确定它是否完全健壮,但它试图解决原始代码中的一些问题(我将在下面提到)。
import os
import sys
import tempfile
import contextlib
def is_case_sensitive(path):
with temp(path) as tmppath:
head, tail = os.path.split(tmppath)
testpath = os.path.join(head, tail.upper())
return not os.path.exists(testpath)
@contextlib.contextmanager
def temp(path):
tmphandle, tmppath = tempfile.mkstemp(dir=path)
os.close(tmphandle)
try:
yield tmppath
finally:
os.unlink(tmppath)
if __name__ == '__main__':
path = os.path.abspath(sys.argv[1])
print(path)
print('Case sensitive: ' + str(is_case_sensitive(path)))
/media/FLASH
下。测试/MEDIA/FLASH
中的任何内容的存在将始终失败,因为/media
位于(区分大小写的)ext4分区上,但闪存驱动器本身不区分大小写。挂载的网络共享可能是另一种类似情况。import os
if os.path.normcase('A') == os.path.normcase('a'):
# case insensitive
else:
# case sensitive
normcase
返回2个不同的结果。 - Romuald Brunet检查路径的大写/小写变体是否存在是有缺陷的。在撰写本文时,有七个答案依赖于相同的策略:从路径(临时文件、主目录或Python文件本身)开始,然后检查该路径的大小写变体是否存在。即使忽略每个目录的区分大小写配置问题,这种方法也是根本无效的。
为什么这种方法在区分大小写的文件系统上会失败。考虑使用临时文件的方法。当tempfile
库返回一个临时文件时,唯一的保证是在创建之前的瞬间,该路径不存在 - 就是这样。如果该路径的文件名部分是FoO
,我们对foo
、FOO
或任何其他大小写变体的存在状态一无所知。诚然,tempfile
库倾向于返回像TmP5pq3us96
这样的名称,它的恶意大小写变体的可能性非常低 - 但我们并不知道。同样的缺陷也影响了使用主目录或Python文件的方法:很可能/HOME/FOO
或/FOO/BAR/FUBB.PY
不存在...但我们没有理由确信。
更好的方法:从你控制的目录开始。一种更健壮的方法是从一个临时目录开始,这个目录在创建时保证为空。在该目录中,您可以执行概念上合理的区分大小写测试。
更好的方法:区分大小写不敏感和大小写保留。对于我正在处理的项目,我需要进行这种区分(并且可以忽略每个目录的大小写敏感设置),因此我最终得出了以下结论。
from functools import cache
from pathlib import Path
from tempfile import TemporaryDirectory
@cache
def file_system_case_sensitivity():
# Determines the file system's case sensitivity.
# This approach ignore the complexity of per-directory
# sensitivity settings supported by some operating systems.
with TemporaryDirectory() as dpath:
# Create an empty temp directory.
# Inside it, touch two differently-cased file names.
d = Path(dpath)
f1 = d / 'FoO'
f2 = d / 'foo'
f1.touch()
f2.touch()
# Ask the file system to report the contents of the temp directory.
# - If two files, system is case-sensitive.
# - If the parent reports having 'FoO', case-preserving.
# - Case-insensitive systems will report having 'foo' or 'FOO'.
contents = tuple(d.iterdir())
return (
'case-sensitive' if len(contents) == 2 else
'case-preserving' if contents == (f1,) else
'case-insensitive'
)
我认为我们可以在Python 3.5+上使用pathlib
一行代码完成此操作,而无需创建临时文件:
from pathlib import Path
def is_case_insensitive(path) -> bool:
return Path(str(Path.home()).upper()).exists()
或者反过来:
def is_case_sensitive(path) -> bool:
return not Path(str(Path.home()).upper()).exists()
/HOME/FOO
,那么这种方法在区分大小写的操作系统上会报告错误。诚然,这是一种奇怪的设置,但我不知道在Linux系统上是否有任何禁止它的东西。 - FMc我相信这是这个问题最简单的解决方案:
from fnmatch import fnmatch
os_is_case_insensitive = fnmatch('A','a')
来自:https://docs.python.org/3.4/library/fnmatch.html
如果操作系统不区分大小写,则在执行比较之前,两个参数都将被规范化为全小写或全大写。
mkstemp()
函数中'prefix'参数的默认值,如代码注释中所述,是"tmp"
(小写)。http://docs.python.org/library/tempfile.html#tempfile.mkstemp 因此生成的文件名将始终以3个小写字符开头。 - Ambergettempprefix() == gettempprefix.lower()
,或在mkstemp()
中明确设置前缀。 - jfsos.path.remove(tmppath)
- Romuald Brunet