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?
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.
Use the str. lower() method to make user input strings case-insensitive, e.g. user_input. lower() .
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With