os.path is not case sensitive under windows

Here is a record of the code performance that is different from your first thought. This kind of thing is still worth recording. Let’s take a look at the code directly:
There is imagea folder named on the desktop with a picture in it.

import os
# os:win10
# This is weird
path1 = 'C:\\Users\\Ezra\\Desktop\\image\\'
path2 = 'C:\\Users\\Ezra\\Desktop\\Image\\'
print('path1 == path2:', path1 == path2)

fileList1 = os.listdir(path1)
fileList2 = os.listdir(path2)

print('fileList1 == fileList2:', fileList1 == fileList2)

Code running results:

path1 == path2: False
fileList1 == fileList2: True

Originally, I thought to access the file, his directory needs to be written very accurately. For example, the upper-level directory of this picture is image. I thought path2it Imagewas inaccessible at first. As a result, I slapped my face and accessed the same content.

analysis

  1. path1 == path2: FalseThis is easy to understand, the string case is definitely different.
  2. The key is whyfileList1 == fileList2: True ? Print it print(os.path.exists(path2)), the result is also True . Is the access path of the os module case-insensitive, so try to change it again:
path3 = 'c:\\users\\ezra\\desktop\\Image\\'
print("path3 is exist:", os.path.exists(path3))
# path3 is exist: True

I wrote suddenly realized that here, previously named when windows install anything D:and d:is the same, only this time they recovered, Windows file (name / folder) is not case-sensitive, and os.paththe module is always running for Python The path module of the operating system is to adapt to the naming principles of different operating systems.

remind

The linux system is case-sensitive to file naming, so you only need to write an accurate path under each operating system, and also pay attention to the migration of projects under different systems or the operation of cross-platform applications. (After I finish writing, I feel like a silly)

Guess you like

Origin blog.csdn.net/laizi_laizi/article/details/105895196