0

Hi I am new to Python and have currently copied following script:

# filename is printer.py
import sys
for arg in sys.argv:
    print arg    # the zeroth element is the module name
raw_input("press any key")    # a trick to keep the python console open

I am trying to get the arguments followed by the module name. But running this code gives me following error:

U:\printer.py
    File "U:\printer.py", line 6
      print arg # zeroth element is the module name
              ^
SyntaxError: Invalid syntax

Does anyone know what could be wrong here? I am using Python 3.2

3 Answers 3

5

In Python 3 print is a function:

print(arg)
Sign up to request clarification or add additional context in comments.

Comments

5

As noted by others, in python3, print is a function:

print arg

is now:

print(arg)

Also, raw_input is gone and replaced by simply input. (in py2k, input would eval the string from the commandline, but it won't do that in py3k).


Finally (and maybe most importantly), have you considered argparse? (untested code follows)

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('foo',nargs='*',help="What a nice day we're having")
args = parser.parse_args()
print(args.foo)

1 Comment

Also, "press any key" is a lie, whether you're using raw_input or input.
2

Python 3 no longer has a print keyword, but a print function.

Try instead

print(arg)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.