I am trying to pass "sys.argv[1]" into a function.
#!/usr/bin/env/ python
import sys
def main():
test(sys.argv[1])
def test(sys.argv[1]):
print "Hello " + sys.argv[1]
./arg.py World
File "./arg.py", line 5
def test(sys.argv[1]):
^
SyntaxError: invalid syntax
Not sure where to go from here after scowering the Interwebs for a few hours. I have also tried to set the "sys.argv[1]" to a variable and tried passing that into the function and still no avail.
I think you misunderstand the declaration and call of a function. In your program,there is only declaration,missing the calling statement. As for passing parameters from command line,here is an example which I prefer:
import sys
def hello(v):
print 'hello '+v
def main(args):
hello(args[1])
if __name__ == '__main__':
main(sys.argv)
The program start to execute from the line 'if name == 'main' by calling the function defined previously and passing the sys.argv as parameter