-
Define a function named dir_count.
This function has one parameter, a path to a directory.
The function should go to the directory specified by the path.
It should then loop through all the entries in the directory
and count the number of directories it finds.
The function should return the number of directories it finds.
You do not have to check if the path actually points to a directory.
def dir_count(path):
os.chdir(path)
total_dirs = 0
entries = os.listdir()
for entry in entries:
if os.path.isdir(entry):
total_dirs += 1
return total_dirs
-
Define the function temps_set_create
which takes a file object as its only parameter.
The function should read in a file with the minimum and maximum temperatures
for each date.
The file will have entries like this
1/1/2021 38 41
1/2/2021 39 45
1/3/2021 40 55
...
The function should create and return a set of all the MINIMUM
temperatures read from this file.
The minimum temperature is the first temperature on each line.
def temps_set_create(file):
temps = set()
for line in file:
fields = line.split()
temps.add(fields[1])
return temps
-
Define the function temps_dict_create
which takes a file object as its only parameter.
The function should read in a file with entries like this
1/1/2021 38
1/2/2021 39
1/3/2021 40
...
It should use these values to create a dictionary where the keys are
dates and the values are temperatures.
It should then return this dictionary.
def temps_dict_create(file):
temps = {}
for line in file:
date, temp = line.split()
temps[date] = temp
return temps
-
Each line below has a username and the name of the person associated
with the username.
Write a regular expression that will match lines like the ones below
and extract the username and full name.
gsmithers George Smithers
dblack David Black
adams Samantha Adams
I am looking for a SINGLE regular expression that matches all lines.
(\w+)\s+(.+)