Python Forum

Full Version: getopt with tuple not working proper
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, i have a piece of code where i try to pass some "ConsoleArguments" to pythons getopt function. The problem is, that i want to fake some user input (argv), but i does not work... Can anyone Help ???

I need to give the getopt function all of my ConsoleArguments as first parameter, but if i do that (it is of type tuple), it seems to cannot start with a "startindex".

I always get following error:

Error:
Traceback (most recent call last): File "HowTo_forloop_with_more_variables.py", line 8, in <module> options, remain = getopt.getopt(ConsoleArguments[0:], 'f:s:a:', ['forename=', 'surname=', 'age=']) File "C:\Python37\lib\getopt.py", line 88, in getopt while args and args[0].startswith('-') and args[0] != '-': AttributeError: 'tuple' object has no attribute 'startswith'
HereĀ“s the Code:

import sys
import getopt


ConsoleArguments = [('-f', 'Michael'), ('-s', 'Frank'), ('-a', '25')]

#If i write ConsoleArguments[0] it works for Element 0, but not with a startingindex like [0:] for all elements
options, remain = getopt.getopt(ConsoleArguments[0:], 'f:s:a:', ['forename=', 'surname=', 'age='])

#Here i want to print all passed Arguments, not only the [0]
for keys, args in options:
    print (keys + ": " + args)
Thanks for Help