Pythonic arguments

We should know by now that we will want to be able to pass command-line arguments to Python and we can do this using the argv array similar to Perl. However, we are more like bash, with Python we combine the program name into the array with the other arguments. Python also uses lowercase instead of uppercase in the object name.

  • The argv array is a part of the sys object
  • sys.argv[0] is the script name
  • sys.argv[1] is the first argument supplied to the script
  • sys.argv[2] is the second supplied argument and so on
  • The argument count will always be at least 1, so, keep this in mind when checking for supplied arguments

Supplying arguments

If we create the $HOME/bin/args.py file we can see this in action. The file should be created as follows and made executable:

#!/usr/bin/python3
import sys
print("Hello " + sys.argv[1])

If we run the script with a supplied argument, we should be able to see something similar to the following screenshot:

Supplying arguments

Our code is still quite clean and simple; however, we may have noticed that we cannot combine the quoted text in the print statement with the argument. We use the + symbol to join or concatenate the two strings together. As there is no specific symbol to denote a variable or any other type of object, they cannot appear as a static text within quotes.

Counting arguments

As it was previously mentioned, the script name is the first argument at index 0 of the array. So, if we try to count the arguments, then the count should always be at the very least one In other words, if we have not supplied arguments, the argument count will be one. To count the items in an array, we can use the len() function. If we edit the script to include a new line we will see this work, as follows:

#!/usr/bin/python3
import sys
print("Hello " + sys.argv[1])
print( len(sys.argv) )

Executing the code as we have earlier, we can see that we have supplied two arguments. The script name and then the string fred:

Counting arguments

If we try and have a single print statement to print the output and the number of arguments, then we will find that Python does not like mixing data types. The length value is an integer and this cannot be mixed with strings without conversion. The following code will fail:

#!/usr/bin/python3
import sys
print("Hello " + sys.argv[1] + " " + len(sys.argv))

However, this is not a mammoth task and just requires an explicit conversion. From the Zen of Python:

"Explicit is better than implicit."

The code will work if modified, as follows:

#!/usr/bin/python3
import sys
print("Hello " + sys.argv[1] + " " + str(len(sys.argv)))

If we try to run the script and omit to supply an argument, then there will be a null value in the array when we reference index 1. This will give an error, as shown in the following screenshot:

Counting arguments

We of course need to handle this to prevent the error and we can now pass into the section of significant whitespace.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.72.15