-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.

2
  • 3
    downvotes without comments teach anything. Commented Jan 23, 2011 at 16:24
  • I'm pretty sure the downvotes are there simply because people did not understand the OP's question..it is not actually about executable for loops, but more how to add command line parameters. Commented Jan 23, 2011 at 16:37

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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...
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
@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.
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:

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.