How is Python able to execute files without the .py extension? Both the files below work. How does Python know that hello.txt
is a Python file?
hello.py
:
print "hello world"
hello.txt
:
print "hello world"
$ python hello.txt
hello world
$ python hello.py
hello world
Python knows it is a Python file because you ran python <name of file>
. File extensions don't actually matter very much to computers. File extensions help you, the human, remember what kind of file something is. They also make it easier for computers to automatically figure out what program to use for a particular file.
But really, file extensions simply don't matter if you want to ignore them. If you tell Python to open a file, it's gonna try to open that file. It doesn't care what the name of the file is; all that matters is if the file contains valid Python code.
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