Python Forum
getopt with tuple not working proper
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getopt with tuple not working proper
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyserial issues with proper loops and binary jttolleson 16 2,464 Nov-02-2023, 08:39 PM
Last Post: deanhystad
  Getting proper x,y axis values pyhill00 8 1,591 Jul-29-2022, 06:48 PM
Last Post: pyhill00
  Proper way to do the OR statement? Mark17 5 1,723 Mar-01-2022, 01:54 PM
Last Post: Mark17
  unpacking tuple not working project_science 1 1,451 Jan-09-2021, 09:09 PM
Last Post: buran
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,727 Nov-04-2020, 11:26 AM
Last Post: Aggam
  (TypeError: 'module' object is not callable) for getopt Module nnevarez 2 2,824 Jul-03-2020, 01:45 AM
Last Post: nnevarez
  proper use of 'end' in a 'with' statement ccrider27 1 2,021 Mar-18-2020, 10:33 PM
Last Post: buran
  Proper use of if..elif..else statement nick1941 2 2,376 Mar-06-2020, 11:22 PM
Last Post: nick1941
  Proper Layout of Code Question TheJax 2 2,148 Feb-08-2020, 06:14 PM
Last Post: TheJax
  Unable to do the proper split using re.sub incase of missing data. Karz 1 1,826 Nov-17-2019, 05:58 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020