Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm using Ubuntu 14.04. And I have an astonish finding today: I can execute a python file without a file extension!

e.g.

$ echo " print 'hello' " > z
$ python z
hello

So, why do we need a file extension? Does it has authentic influence to the file other than tell people semantically what file this is?

share|improve this question
3  
Do you mean file extension by postfix? They are not needed on Linux, but they're important on MS Windows, when you want to call the script without preceding it with python. –  choroba Jun 17 at 11:19
1  
Same as chobora - are you talking about the file extension? by calling 'python' it's almost redundant to need to add '.py', calling python should assume you're working with a python file. –  root Jun 17 at 11:20
    
@choroba, yes, I mean the extensions –  Zen Jun 17 at 13:14
    
Windows does not "need" extenstions to run files (you can use the exact same syntax above e.g. "python z" assuming "python" is a available to Windows), but Windows will not open files automatically with a particular program without extensions (you have to manually select the program -- which can become cumbersome in the Windows gui). –  Anaksunaman Jul 6 at 2:54

1 Answer 1

up vote 2 down vote accepted

If you are talking about the extension, Linux does not use extensions the same way Windows does, in fact, extensions have no meaning at all in Linux and are just part of the filename. They are still being used a lot to make it easier for people to recognize files. One common occurrence where extensions are not being used is with binary executables (you can use python instead of python.exe).

There are two ways of running scripts:

  • Invoking from shell (./scriptname.py): In the script, the first line will say something along the lines of #!/usr/bin/python. The #! instructs the kernel to use the subsequent file as interpreter for the script.

    From http://en.wikipedia.org/wiki/Shebang_%28Unix%29

    The shebang is actually a human-readable instance of a magic number in the executable file, the magic byte string being 0x23 0x21, the two-character encoding in ASCII of #!. This magic number is detected by the "exec" family of functions, which determine whether an image file is a script or an executable binary. The presence of the shebang will result in the execution of the specified executable, usually an interpreter for the script's language.

  • Invoking the interpreter with script as argument (python scriptname). If you do this, you start the interpreter, and you tell it explicitly what script to run. The script can be any file, with any 'extension'.

share|improve this answer
    
One can also use this line #!/usr/bin/env python, which will find python regardless of where it is installed. (Env will always be in /usr/bin and thus using that rather than a direct path is much prefered when one wants portability). –  Hennes Jul 6 at 1:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.