Understanding Command Line Arguments by making a simple file searcher

joshthecodingaddict

Joshua Hassan

Posted on April 12, 2022

Understanding Command Line Arguments by making a simple file searcher

The sys.argv Array

There are various ways of handling Python command line arguments, but what you need to know is that the underlying support for all Python command line arguments is provided by sys.argv.

The examples will show you how to handle the python command line arguments stored in sys.argv and to solve a few problems that come when trying to access them.

We will learn how to

  • Access the contents of sys.argv
  • Process whitespaces in python command line arguments
  • Handle errors while processing python command line arguments

Displaying Arguments

The sys module exposes an array named argv that includes the following:

  1. argv[0] contains the name of the current Python program.
  2. argv[1:], the rest of the list, contains any and all Python command line arguments passed to the program.

for example

Code sample showing sys.argv
Line 1 imports the python module sys
Line 3 gives the name of the program by getting the first item in the list sys.argv
Line 5 displays the python command line arguments by getting all the remaining items of the list sys.argv

Running the script above with a list of arguments as shown



python argv.py cat dog sheep friends eleven
Name of script:                argv.py
Arguments passed to script:    ['cat', 'dog', 'sheep', 'friends', 'eleven']


Enter fullscreen mode Exit fullscreen mode

Code sample showing output
The output confirms that the content of sys.argv[0] is the Python script argv.py, and that the remaining elements of the sys.argv list contains the arguments of the script, ['cat', 'dog', 'sheep', 'friends', 'eleven'].

Escaping Whitespace Characters

On Linux, whitespaces can be escaped by doing one of the following:

  1. Surrounding the arguments with single quotes (')
  2. Surrounding the arguments with double quotes (")
  3. Prefixing each space with a backslash () Without one of these solutions, the script will store two arguments instead of one as in the example below


C:\Users\UserName\Desktop\DevPosts>python argv.py "cat dog"
Name of script:                argv.py
Arguments passed to script:    ['cat dog']

C:\Users\UserName\Desktop\DevPosts>python argv.py cat dog
Name of script:                argv.py
Arguments passed to script:    ['cat', 'dog']


Enter fullscreen mode Exit fullscreen mode

Code smaple showing output

Handling Errors

Many things can go wrong, so it’s a good idea to provide the users of your program with some guidance in the event they pass incorrect arguments at the command line. For example, kill.py expects one argument, and if you omit it, then you get an error:



Traceback (most recent call last):
  File "C:\Users\UserName\Desktop\DevPosts\searcher.py", line 4, in <module>
    print("File To Search:                {}".format(sys.argv[1]))
IndexError: list index out of range


Enter fullscreen mode Exit fullscreen mode

Code sample showing output 4
The Python exception IndexErroris raised, and the corresponding traceback shows that the error is caused by the expression arg = sys.argv[1]. The message of the exception is list index out of range. So you didn’t pass an argument at the command line, so there’s nothing in the list sys.argv at index 1.

This is a common problem type that can be addressed in a few different ways. For this initial example, you’ll keep it brief by including the expression arg = sys.argv[1] in a try block. Modify the code as follows:



import sys

try:
    file_to_search = sys.argv[1]
except IndexError:
    raise SystemExit("Usage: {} <file_to_search>".format(sys.argv[0]))



Enter fullscreen mode Exit fullscreen mode

The expression is included in a try block. Line 6 raises the built-in exception SystemExit. If no argument is passed to searcher.py, then the process exits with a status code of 1 after printing the usage. Note the addition of sys.argv[0] in the error message. It shows the name of the program in the usage message. Now, when you run the same program without any Python command line arguments, you can see the following output:



C:\Users\UserName\Desktop\DevPosts>searcher.py
Usage: C:\Users\UserName\Desktop\DevPosts\searcher.py <file_to_search>


Enter fullscreen mode Exit fullscreen mode

Now that we have made ourselves familiar with how sys.argv works, we can dive into our little project which is to code a simple file searcher

first we import the sys module



import sys


Enter fullscreen mode Exit fullscreen mode

Next we write checks to make sure we get the right number of arguments from the user



if len(sys.argv) < 3:
    sys.stderr.write("Error: Usage " + sys.argv[0] + " <filename> <words>")
    sys.stderr.flush()
    exit(2)
else:
    filename = sys.argv[1]
    needle = sys.argv[2]


Enter fullscreen mode Exit fullscreen mode

initalize counter for the number of times we find our needle



counter = 0


Enter fullscreen mode Exit fullscreen mode

Now we open our file in read mode



file_handler = open(filename)


Enter fullscreen mode Exit fullscreen mode

Next step is to iterate through the file while separating words in each line by a delimeter (space) and making it into a sentence



for line in file_handler.readlines():
        sentences = line.split(" ")


Enter fullscreen mode Exit fullscreen mode

Then iterate through the sentences word by word and compare against our needle, and if it is a match, we increment counter



for word in sentences:      
        # this checks if the last word is a new line character and eliminates it        
    if word == sentences[-1]:
        word = word[:-1]            
    if word == needle:              
        counter += 1


Enter fullscreen mode Exit fullscreen mode

Finally we print the number of times the word was found



print (str(counter) + " times " + needle + " appears")


Enter fullscreen mode Exit fullscreen mode

The full code



import sys

if len(sys.argv) < 3:
    sys.stderr.write("Error: Usage " + sys.argv[0] + " <filename> <words>")
    sys.stderr.flush()
    exit(2)
else:
    filename = sys.argv[1]
    needle = sys.argv[2]

counter = 0
file_handler = open(filename)

for line in file_handler.readlines():   
    sentences = line.split(" ")         
    for word in sentences:              
        if word == sentences[-1]:       
            word = word[:-1]            
        if word == needle:              
            counter += 1                

print (str(counter) + " times " + needle + " appears")


Enter fullscreen mode Exit fullscreen mode

In this tutorial, you've learned about Python command line arguments. You should feel a little prepared to apply this new skills to your code.

Happy Coding :)

💖 💪 🙅 🚩
joshthecodingaddict
Joshua Hassan

Posted on April 12, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related