Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python treat files with uppercase and lowercase names the same

I just found out this today:
If I have an existing file named a111, and I want to create a new file named A111 in the same directory with Python:

f = file('A111', 'w')
f.write('test')
f.close()

It will overwrite my file a111 and there's no A111!!
How do I prevent this from happening?

like image 595
BPm Avatar asked Feb 09 '12 00:02

BPm


People also ask

Does uppercase and lowercase matter in Python?

Variables can only contain upper and lowercase letters (Python is case-sensitive) and _ (the underscore character). Hence, because we can't have spaces in variable names a common convention is to capitalize the first letter of every word after the first.

How do I make Python not case-sensitive?

Use the str. lower() method to make user input strings case-insensitive, e.g. user_input. lower() .

Is Python case-sensitive What is meant by the term case-sensitive?

Is Python a Case-Sensitive Language? YES, Python is a case-sensitive programming language. This means that it treats uppercase and lowercase letters differently. Hence, we cannot use two terms having same characters but different cases interchangeably in Python.


1 Answers

It is not due to python. It is due to the case-insensitivity of your underlying file system (I'm guessing HFS+ in your case?). From wikipedia:

Not all file systems in Unix-like systems are case-sensitive; by default, HFS+ in Mac OS X is case-insensitive

The solution is to use a case-sensitive file system, if you want one, or to use a different filename!

like image 131
wim Avatar answered Sep 19 '22 03:09

wim