6

I am new to python. I wrote a program which can be executed by typing python Filecount.py ${argument} Why I see my teacher can run a program by only typing Filecount.py ${argument}. How to achieve that?

3

3 Answers 3

11

Make it executable

chmod +x Filecount.py 

and add a hashbang to the top of Filecount.py which lets the os know that you want to use the python interpreter to execute the file.

#!/usr/bin/env python 

Then run it like

./Filecount.py args 
1
  • 4
    /usr/bin/env is preferred to /bin/python. There is not even a proper /bin directory on my system (this would resolve through a symlink). Also, on certain Arch-based distros, this would resolve to Python 3, not Python 2, whereas I believe the /usr/bin/env style resolves as expected.CommentedMar 25, 2014 at 16:02
2

in linux-based OSs you must include a line (at the beginning of your script, i.e., the first line) like this:

#!/usr/bin/python 

this tells the OS to seek your python interpreter at that location. this applies to any script.

remember to have the permissions in your script file (i.e. executable) for that to work.

1
  • As I pointed out on Andre's answer, the preferred shebang line is /usr/bin/env python or equivalent, rather than specifying the path where you suppose Python exists on the system.CommentedMar 25, 2014 at 16:03
2

Add a shebang line to the top of your file: http://en.wikipedia.org/wiki/Shebang_(Unix)#Purpose

It will tell the system which executable to use in running your program.

For example, add

#!/usr/bin/env python 

as the first line, and then change the permissions of the file so you can execute it.

chmod +x Filecount.py 

Best of luck!

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.