Python Forum
Not all data returned from sys.argv - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Not all data returned from sys.argv (/thread-20904.html)



Not all data returned from sys.argv - ecdhyne - Sep-05-2019

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)



RE: Not all data returned from sys.argv - Axel_Erfurt - Sep-05-2019

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



RE: Not all data returned from sys.argv - buran - Sep-05-2019

@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