-2

I've created this working for loop in Python:

a = 'word'

for i in range(len(a)):

 print a[i:] + a[:i], (a[i:] + a[:i])[::-1]

and I need to make it executable from the command line and also have it be able to take in new arguments.

I'm a little hazy on how to do this. I know that you need to use

if __name__ == '__main__':

somehow...And that I need to use sys.argv

If anyone could explain how to do this, and the format for making any script executable from the command line, I would really appreciate it.

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
SexyLlama
  • 11
  • 3

3 Answers3

2

You can just simply write

import sys
a = sys.argv[1]
etc..

And then run:

python yourcode.py argument

The

if __name__=='__main__':

is not required, but you can also put your whole code under that. It's purpose is to specify code which gets executed only if you run your program like "python yourcode.py" and to prevent the code under that if statement from getting executed if you write "import yourcode" in another .py file.

kynnysmatto
  • 3,665
  • 23
  • 29
  • I need to be able to run the script without having to type import sys...So I guess "import sys" needs to be part of my code. I think that's why if __name__ == '__main__': is required... – SexyLlama Jan 23 '11 at 16:28
  • I'm not aware of a way to get the command-line arguments without importing sys. But that itself doesn't have anything to do with name==main – kynnysmatto Jan 23 '11 at 16:34
  • 1
    @SexyLlama: The first code snippet in this answer is meant to be placed at the beginning of your Python code. And remember to substitute the `etc..` bit by your for loop. – Sven Marnach Jan 23 '11 at 16:41
2

Depending on how flexible you want to have your Python script, I would structure the script like so:

def get_args():
    # logic for parsing arguments here
    # return e.g. a dictionary

def your_method(arg1=None,arg2=...):
    # further logic

if __name__ == "__main__":
    args = get_args()
    your_method(**args)

There are various modules to parse command line arguments. Have a look at argparse (easier) and optparse.

If you just need a simple way to access the command line arguments, you can go with sys.argv.

With this separation you are also able to import your function into other code.

Example:

import sys

def get_args():
    word = sys.argv[1] if (len(sys.argv) > 1) else ''
    return {"word": word}

def your_method(word=''):
    for i in range(len(word)):
        print word[i:] + word[:i], (word[i:] + word[:i])[::-1]

if __name__ == "__main__":
    args = get_args()
    your_method(**args)

and run it with

python yourscript.py someword

See also:

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0
import sys
if __name__ == '__main__':
    a = sys.argv[1] #sys.argv[0] is your file name, and [1] is the next argument
    for i in range(len(a)):
        print a[i:] + a[:i], (a[i:] + a[:i])[::-1]

Then you simply change to the directory with the file, and call it like:

python file.py wordHere

On the command line.

PrettyPrincessKitty FS
  • 6,117
  • 5
  • 36
  • 51