1

I have one python file (abc.py) which include several commands like make directory, copy commands. I want to execute it such like that,whenever I hit command for example abc --makedir on console, it should make directory. makedir is function which is written in abc.py.

Yagya
  • 29
  • 1
  • 1
  • 8
  • I had tried adding it in a path variable but it does not works. – Yagya Jan 27 '16 at 09:29
  • Where is your code and what's the problem? – luoluo Jan 27 '16 at 09:31
  • This sounds like you should use the shell, not Python. – tripleee Jan 27 '16 at 09:36
  • The file name, including any extension, is how you invoke a script. If you want the command to be named `abc` then the file should be named `abc`. There is no requirement to have a `.py` extension on a Python script in Unix; in fact, for anything you want to use as a direct command, it's probably not a good idea. Just make sure you give the file a proper shebang line like `#!/usr/bin/env python` on the very first line of the file, and make sure the file is executable, and that the directory it's in is on your `PATH`. – tripleee Jan 27 '16 at 09:38
  • there is a python file named abc.py and containing a function name makedir() which when called makes a new directory. i want to execute as abc --makedir it should make a new directory. – Yagya Jan 27 '16 at 09:43

1 Answers1

1

Rename abc.py to abc.

Make it executable:

chmod +x abc

Then add this at the first line of your script:

#!/usr/bin/python

From command line (if abc is in python path):

#abc

To create a directory like you said , you should parse arguments passed to python script.

For example:

import sys

if len(sys.argv)>1:
    if sys.argv[1] == '--makedir':
        makedir()

For more informations Look at this link What's the best way to grab/parse command line arguments passed to a Python script?

Community
  • 1
  • 1
Kenly
  • 24,317
  • 7
  • 44
  • 60