Python Doesn't Care About The Extension!
Darshan-Bajeja
Posted on December 9, 2021
Once upon a time (don't go away, this isn't a story!), I wrote a python script to generate random phone numbers to as mock data for testing my express API (Yes Python helping JS!). I thought that it was a very simple script, so why not write it out in notepad instead of something like VS Code or PyCharm. So I wrote it down, and by mistake I saved it as text file and not python file (main.txt), and I even ran it. So as a dev, very quickly I opened up Powershell, just wrote cd Des
, hit tab and enter. Then just wrote python m
, and a quick tab and enter and to my surprise I got an error. Now you might say that, ok you ran a text file with python, what else do you expect except an error. But guys, the error wasn't that, the error was related to a bug in my script! And as I mentioned, I just typed python m
and hit tab, it showed python main.txt
, and I was surprised with that. Python running a text file!
Then I thought Programming languages just see the text in the file and don't care about the extension. So I tried a similar program in Java, and it didn't work... So I thought may be its just with Python. Now I didn't try it out with other languages, but I am sure that Python doesn't care about your file extension. I tried the same with .mp3
file as well and it worked!!!
So let's try it now:
Open up the simplest text editor possible on your machine, that is Notepad on Windows, TextEdit on MacOS, or Gedit, Mousepad, or Text Editor on Linux.
Write any python code inside it. I will write the code to take a number as input and print out its square.
- Now let's save this file, but give any other extension except .py or .pyc as they are for Python specifically. I will give it the extension of sqlite3. (Yooo!)
- Finally use any CLI to run the code...
And it works....
Now I don't have a good conclusion for this post, but I know you enjoyed the content, and liked the fun fact...!!
And yes, if any of you pythonistas know the reason for this, please put down in comments below!
Posted on December 9, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.