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 B Lab 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 genlabfiles.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 recursivelyderived lists of files, return this list.
Read File
This nonrecursive 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 nonrecursive function should perform two tasks. The first is to call discoverfiles 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 readfile
Then each list should be combined into a single lista of all tuples. And here is my genlab code:import ospath
import random
nameopts bark 'yip', 'awoo', 'chirp', 'purr', 'meow', 'growl', 'hiss', 'yap', 'scree', 'bork'
def genfilespath: str prefix: str maxnum: int, sizemetric: int, depth: int:
if not ospath.isdirpath:
if ospath.existspath:
return
else:
osmkdirpath
numgen maxrandomrandint maxnum depth,
dirnames random.choicesnameopts, knumgen
filenames random.choicesnameopts, krandom.randint maxnum
for dname in dirnames:
newpath ospath.joinpath dname
genfilesnewpath, prefix, maxnum, sizemetric, depth
for fname in filenames:
fullfname ospath.joinpath fname prefix
with openfullfname, w as file:
for i in rangerandomrandintintsizemetric sizemetric:
file.write
joinrandomchoicesnameopts, krandom.randintintsizemetric sizemetric
if namemain:
genfilesaniwordsdog.txt
and how to solve this by:import os
def discoverfilespath: str extension: str liststr:
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 filesdirectories 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 readfilefilepath: str listtuplestr 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 filename: str linenumber: int, linetext: str
Line text should have any newline characters removed.
:param filepath: The file to read data from.
:return: a list of tuples of data from the file
return
def readdirectorypath: str extension: str listtuplestr 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
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
