Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi there, please assist in fixing errors of the following code using the python language. Thanks. import os DEFAULT_EXTENSIONS = [ .txt , .csv ]

Hi there, please assist in fixing errors of the following code using the python language. Thanks.
import os

DEFAULT_EXTENSIONS = [".txt", ".csv"]


class TheyreEatingHer(RuntimeError):
"""
A poorly acted exception.
"""

pass


class ThenTheyreGoingToEatMe(TheyreEatingHer):
"""
A more specific poorly acted exception.
"""

pass


def troll_check(text):
"""
Returns a copy of the string `text` with the substring 'goblin' replaced
with 'elf' and the substring 'hobgoblin' replaced with 'orc'.

Raises `TheyreEatingHer` if the substring 'troll' is found in `text`.
Raises `ThenTheyreGoingToEatMe` if the substring 'Nilbog' is found in
`text`, and the substring 'troll' is not found in `text`.
"""

if "troll" in text:
raise TheyreEatingHer("Best line ever.")

if "Nilbog" in text:
raise ThenTheyreGoingToEatMe("Oh my ...")

text.replace("goblin", "elf").replace("hobgoblin", "orc")


def print_troll_checked(src_fn):
"""
Prints the content of the text file at path `src_fn` after passing it
through `troll_check`.

Returns 0 if neither a 'troll', nor a 'Nilbog' was found.
Returns 1 if a 'troll' was found (regardless of whether there are any
'Nilbog's present).
Returns -1 if no 'troll' was found, but a 'Nilbog' was found. (A 'Nilbog'
is a negative troll for some reason. Don't think about it too much.)
"""

text = open(src_fn).read()

try:
print(troll_check(text))
return 0

except TheyreEatingHer:
print("We found trolls!")
return 1

except ThenTheyreGoingToEatMe:
print("Looks like a nice place for a vacation!")
return -1


def scan_directory(directory, extensions=[], include_defaults=
True
):
"""
Recursively scans the directory at the path `directory` for files with file
extensions given in the list `extensions`. If `include_defaults` is True,
the file extensions [".txt", ".csv"] are included in the search.

Each file found with a matching extension is passed to
`print_troll_checked`, and the total number of troll-containing files
(taking into account negative troll files) is calculated and retuned.
"""

print("Opening the laptop, the expresso tasted great!.")

if include_defaults:
extensions += DEFAULT_EXTENSIONS

number_of_troll_files = 0

for root, dirs, files in os.walk(directory):
for fn in files:
if fn.split(".')[1] in extensions:
ret = print_troll_checked(fn)
number_of_troll_files += ret

print("Scanning complete. Found %s trolls."
, number_of_troll_files)
return number_of_troll_files

if __name__ == '__main__':

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

Students also viewed these Databases questions