Lab 11: Programs that take command line arguments
Write a Python program that does counting of words and sentences in a file. The program should accept multiple file names when it is invoked and duely process them.
Examples
wordcount("python"
) -> 1 # 1 word and no sentences
wordcount("python is great language")
-> 4 # 4 words and no sentences
wordcount("Sentence 1. Sentence 2.")
-> (4, 2) # 4 words and 2 sentences
Solution Key
Pre-Lab Questions
Post-Lab Questions
Modify the version (make suitable changes to
wordcount.py file
) available in http://cyberdojo1.kgfsl.com/enter/show/3080E868E6 so thatThe total sentences in all the files to be calculated and displayed
If the filename with the specified command line argument is not available, print "File not available." by using the appropriate named exception.
If there are other errors, catch them as well and print "Unknown error!"
Refactor the solution to the Question_1 to use
getopt
and handle the exceptions for handling options flags in an appropriate manner. For example, choose a option flag which can be used to denote which file can be updated with the results of the processing.Provide support for processing adhoc strings through the command line option. For example, when program is invoked with
-s
option,wcount.py -s "This is a sentence. Another one." file1.txt
It must process the string within quotes and also the contents within the file1.txt
file as well.
Bonus
Make your program to behave as close as possible to the Unix command wc
.
See the most important examples at https://www.tecmint.com/wc-command-examples/
Bonus 2
How to test all the various refactorings that were done as part of the post-lab Questions?
Related Material
Using getopt
Basic command line argument handling
CD Links
Last updated