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?

Paulo Bu
  • 27,056
  • 6
  • 67
  • 69
hidemyname
  • 2,487
  • 4
  • 22
  • 39

3 Answers3

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
André Laszlo
  • 13,970
  • 2
  • 60
  • 73
  • 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. – Two-Bit Alchemist Mar 25 '14 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.

Luis Masuelli
  • 10,846
  • 9
  • 41
  • 80
  • 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. – Two-Bit Alchemist Mar 25 '14 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!

mattwise
  • 1,406
  • 1
  • 10
  • 19