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)
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
@
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