DEV Community 👩‍💻👨‍💻

Darshan-Bajeja
Darshan-Bajeja

Posted on

Python Doesn't Care About The Extension!

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:

  1. 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.

  2. Write any python code inside it. I will write the code to take a number as input and print out its square.

Python code...

  1. 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!)

Python in SQL

  1. Finally use any CLI to run the code... Python code runs with sqlite3 extension!

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!

Top comments (2)

Collapse
 
soufyaneyassin profile image
soufyane

hello, thank you for sharing, i think this post may help you to understand why python doesn't care for extensions:
stackoverflow.com/questions/319262...

Collapse
 
darshanbajeja profile image
Darshan-Bajeja

Thanks!

We want your help! Become a Tag Moderator.
Fill out this survey and help us moderate our community by becoming a tag moderator here at DEV.