Python Forum
Not all data returned from sys.argv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not all data returned from sys.argv
#1
In my password lookup pgm sys.argv only returns the pgms file location. How do I get the selected input?
#! python 3
# pw.py - password lookup pgm
PASSWORDS = {'hotmail': 'ECDaug#2019',
             'isimail': 'ECDaug#2019',
             'tdadmin': 'ecdisi04'}

import sys, pyperclip
print (sys.argv)
print ('This is the name of the script: '), sys.argv[0]
print ('Number of arguments: '), len(sys.argv)
print ('The arguments are: ') , str(sys.argv)
Reply
#2
example (file tmp.py)

import sys

if __name__ == '__main__':
    print(len(sys.argv))
    print(sys.argv[0])
    print(sys.argv[1])
    print(sys.argv[2])
start with:

python3 tmp.py first second

Output:
3 tmp.py first second
Reply
#3
@ecdhyne
line 8 should print list with file name and all CLI arguments

The problem with lines 9-11 is the closing place where you put closing parenthesis of print function. As is now each line is just at 2-element tuple (so a valid code)
let's look at line 9
print ('This is the name of the script: '), sys.argv[0]
it execute the print function (everything before the comma)
print ('This is the name of the script: ')
print function returns None
here the line in interactive mode
>>> import sys
>>> print ('This is the name of the script: '), sys.argv[0]
This is the name of the script: 
(None, '')
>>> print ('Number of arguments: '), len(sys.argv)
Number of arguments: 
(None, 1)
>>> print ('The arguments are: ') , str(sys.argv)
The arguments are: 
(None, "['']")

lines 9-11 should be

print ('This is the name of the script: ', sys.argv[0])
print ('Number of arguments: ', len(sys.argv))
print ('The arguments are: ', str(sys.argv))
or better use string formatting, e.g. str.format() or f-strings
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to access values returned from inquirer cspower 6 700 Dec-26-2023, 09:34 PM
Last Post: cspower
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 753 May-02-2023, 08:40 AM
Last Post: Gribouillis
  sys.argv method nngokturk 3 1,027 Jan-23-2023, 10:41 PM
Last Post: deanhystad
  SQLAlchemy Object Missing when Null is returned Personne 1 1,677 Feb-19-2022, 02:50 AM
Last Post: Larz60+
  Getting "name 'get_weather' is not defined error and no json_data returned? trthskr4 6 3,528 Sep-14-2021, 09:55 AM
Last Post: trthskr4
  Libraries installed with pipenv, but ModuleNotFoundError returned jpncsu 2 2,946 Sep-06-2021, 07:24 PM
Last Post: jpncsu
  import argv Scott 3 5,838 Apr-01-2021, 11:03 AM
Last Post: radix018
  Exception: Returned Type Mismatch Error devansing 1 5,089 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  How to use a returned value? t4keheart 12 4,751 Jan-16-2020, 06:54 AM
Last Post: perfringo
  Strange Characters in JSON returned string fioranosnake 4 5,120 Dec-02-2019, 07:25 PM
Last Post: fioranosnake

Forum Jump:

User Panel Messages

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