Question: CS 0 0 9 B - Lab 4 - Recursive File Read In this lab, you will be writing a recursive function to walk a

CS 009B - Lab 4- Recursive File Read
In this lab, you will be writing a recursive function to walk a directory tree and find files of a
particular extension. These files all have the same structure, being a .csv (comma separated
list). You will also then load the data from each of these files into one single data structure that
contains all of the row data from every file, along with the file path at which you found that row.
To start, run the script gen_lab4_files.py. This script recursively generates dummy files
with random names and data inside a random tree structure. This randomly generated directory
is a good way to look at the problem you will need to solve and will also serve as a good way to
test your code. Additionally, reading the code may give you a hint towards writing your solution.
Discover Files
This function is the one recursive function of this assignment. The other two functions are not
recursive. This function takes a particular path and returns a list of files in this directory,
including files inside subdirectories.
This function should list the contents of the directory and then do two things.
- For the files, add their paths to a list.
- For the directories, recursively call this function on each directory. For the returned list,
append it to the running list.
After combining the direct list and the recursively-derived lists of files, return this list.
Read File
This non-recursive function reads a file at a given path and returns a list of tuples describing the
data. Each line is described by a tuple with the following data:
- String: Path of file that the line came from (should be the same for all tuples generated
by a particular call to this function.
- Int: The line number of this line.
- String: The contents of this line.
Read Directory
This non-recursive function should perform two tasks. The first is to call discover_files() to obtain
a full list of all files in a directory.
Next, this function should iterate through each file path and read each file using read_file().
Then each list should be combined into a single lista of all tuples. And here is my genlab code:import os.path
import random
name_opts =['bark', 'yip', 'awoo', 'chirp', 'purr', 'meow', 'growl', 'hiss', 'yap', 'scree', 'bork']
def gen_files(path: str, prefix: str, max_num: int, size_metric: int, depth: int):
if not os.path.isdir(path):
if os.path.exists(path):
return
else:
os.mkdir(path)
num_gen = max(random.randint(1, max_num)- depth, 0)
dir_names = random.choices(name_opts, k=num_gen)
file_names = random.choices(name_opts, k=random.randint(1, max_num))
for dname in dir_names:
new_path = os.path.join(path, dname)
gen_files(new_path, prefix, max_num, size_metric, depth +1)
for fname in file_names:
full_fname = os.path.join(path, fname + prefix)
with open(full_fname, 'w') as file:
for i in range(random.randint(int(size_metric /2), size_metric)):
file.write(
''.join(random.choices(name_opts, k=random.randint(int(size_metric /2), size_metric)))+'
')
if __name__=='__main__':
gen_files('aniwords','.dog.txt',5,20,0)
and how to solve this by:import os
def discover_files(path: str, extension: str)-> list[str]:
"""
Recursively discovers files in a path and returns a list of valid file paths
from which data can be read. Should only return files with a matching extension.
The extension parameter will be something like '.csv' or '.dogs.csv'
As a hint, this function should iterate though the files/directories in the target directory
and then call itself on each directory discovered.
This function should return a combined list of files found in this directory as well as
any files found by recursive calls on subdirectories.
:param path: the directory path to search
:param extension: the extension to match
:return: a list of file path strings, discovered recursively
"""
return []
def read_file(file_path: str)-> list[tuple[str, int, str]]:
"""
Read the lines from a file. Creating a list of tuples with file path information and line information
Each tuple should be in the format (file_name: str, line_number: int, line_text: str).
Line text should have any newline characters removed.
:param file_path: The file to read data from.
:return: a list of tuples of data from the file
"""
return []
def read_directory(path: str, extension: str)-> list[tuple[str, int, str]]:
"""
Reads a directory recursively and returns a list of tuples.
The first element is the file name, the second is the line number (starting from 1),
and the last element is the string content of that line (without a newline character).
:param path: the directory path to search
:param extension: the extension to match
:return: a list of tuples
ireallydont know houw to do this.Could yo

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!