Question
The program should do the following:
1. Ask the user for three things
a. A directory that to search (it may contain subdirectories within it)
b. A regular expression to match the filename
i. Matches are against the FULL filename, and not just the name within the directory
ii. So, if the user types in “b” as the regular expression, the following files would match
1. /dir1/dir2/bat.txt
2. /dir1/dir2/file.bmp
3. /dir1/backup/file1.txt
c. A regular expression to match lines within the file
2. Search all files within the directory, and traverse any subdirectories in it
3. Handle .tar or .zip files within the directory
a. For full-credit, you must handle one of these
b. Extra credit will be given if you can handle both of these
c. For a small “penalty”, you may elect not to handle neither
i. (if you do so, you can only earn 10 out of the max 12.5 points in Specification)
d. You do not need to handle .zip and .tar files within .zip and .tar files
i. (So no recursion is necessary)
4. Indicate the full filename (aka the whole path) to the file, a colon and a space, and the line it matched.
a. See the sample runs on the next page
5. You may use Python, Java, or any other language with prior instructor approval.
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
import os # os module for walking directoriesimport re # re module for regex
import tarfile # tarfile module for reading .tar files
import zipfile # zipfile module for reading .zip files
# Main method
def main():
# Query directory
directory = input('Enter directory to search : ')
# Query filename regex
filename_regex = input('What files do you want to match (use RegExp) : ')
# Query line regex
line_regex = input('What lines do you want to match (use RegExp) : ')
# Gather all files and directories in directory
for root, dirnames, filenames in os.walk(directory):
# Iterate over filenames
for filename in filenames:
filename = os.path.join(root, filename)
# Check .tar files
if filename[-4:] == '.tar':
tar = tarfile.open(filename, mode='r')
# Check files inside of .tar file
for tarmember in tar.getmembers():
tarfilename = os.path.join(filename...