尝试使用 open(filename,'x') 打开它时文件存在错误 - Windows 小写和大写的大小写敏感问题

File exists error when trying to open it with open(filename,'x') - Windows case sensitivity problem with lower and uppercase

我在 Windows,我的目录中存在 none 个文件。

我无法弄清楚原因:

fid = open('L01A.txt', 'x')
fid.write('A') 
fid.close()

fid = open('L01a.txt', 'x')
fid.write('a')
fid.close()

给我:

[Errno 17] File exists: 'L01a.txt'.

您使用 'x' 模式打开文件,该模式仅用于创建文件。来自文档

'x', open for exclusive creation, failing if the file already exists

你应该使用另一种模式,这里有一个有用的link对可能对你有用的不同模式的描述

python open built-in function: difference between modes a, a+, w, w+, and r+?

编辑:显然你的错误是你不能创建名称为 L01AL01a 的两个不同大小写的文件,这是 windows 文件系统不区分大小写。您不能创建两个不同的文件。

如果您绝对需要区分大小写,您可以在目录中启用 NTFS,启动管理员 powershell 并执行 fsutil.exe file setCaseSensitiveInfo C:\folder enable

根据此线程,您可能希望为所有子目录启用此功能,这是一种方法

感谢 Lalush 的主题。