Question: Regex search within files Skills and elements used File manipulation, input validation, regular expressions Program specification Write a program that takes the name of a
Regex search within files
Skills and elements used
File manipulation, input validation, regular expressions
Program specification
Write a program that takes the name of a text file and a regular expression from the command line and
displays any lines from the text file that match the regular expression along with the line number. After
your program is finished be sure and close your open file.
Note: You will probably need to put quotes around your regular expression if it contains special
characters such as backslashes.
Verify that the user has passed in the proper arguments and display an error if they have not.
If the file does not exist or cannot be opened display an error.
Example
$ python
search.py
Error: You must supply a filename and regex
Ex python
search.py file.txt btestb
$ python
search.py file.txt btestb
: The subject took the test and score an
: since the test turned out to be meant for
Note: and are the line numbers where the text appears within
file.txt
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
