Skip to content

Commit

Permalink
small change in files
Browse files Browse the repository at this point in the history
  • Loading branch information
christiaanwinchub committed Jul 20, 2023
1 parent e07996f commit fdfd160
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion wincpy/checks/ae539110d03e49ea8738fd413ac44ba8.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def check_clean_cache(student_module):

cache_path, _ = __get_paths(student_module)
student_module.clean_cache()
assert os.path.isdir(cache_path), f"`cache_path` did not create {cache_path}"
assert os.path.isdir(cache_path), f"The cache directory was not created properly."

# Put some bogus file in it that should be gone after calling clean_cache()
with open(os.path.join(cache_path, "throwaway"), "w") as fp:
Expand Down
Binary file not shown.
91 changes: 91 additions & 0 deletions wincpy/solutions/ae539110d03e49ea8738fd413ac44ba8/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Do not modify these lines
__winc_id__ = "ae539110d03e49ea8738fd413ac44ba8"
__human_name__ = "files"

# Add your code after this line

import os
import shutil
from zipfile import ZipFile

###
# --- Make sure the imports are on the top of the file ---
###

base_path = os.getcwd()
cache_path = os.path.join(base_path, "cache")
data_path = os.path.join(base_path, "data.zip")
"""
Here we create the paths we use in the assignment as global variables, this way you only have to define them once.
Create the paths using "getcwd()" and "join()" so the paths can be used on every computer and OS.
"""

# 1

def clean_cache():
if os.path.exists(cache_path):
shutil.rmtree(cache_path)
os.mkdir(cache_path)

###
# If "cache" exists, delete the directory. Then make a new directory.
###

# 2

def cache_zip(zip, cache):
with ZipFile(zip, "r") as zipObj:
zipObj.extractall(cache)

###
# Use the zipfile module to extract the zip file in a small amount of code.
###

# 3

def cached_files():
cached_files_list = []
for path in os.listdir(cache_path):
full_path = os.path.join(cache_path, path)
cached_files_list.append(full_path)
return cached_files_list
"""
Here we do the following:
- Use listdir to loop over the name of every file.
- Make the full path with "join()"
- Use "append()" to add the path to a predefined list
- return the list
Note: We can also use a list comprehension as seen below. Read this as:
'do something for every value in list'
"""
def cached_files_alternative():
return [os.path.join(cache_path, path) for path in os.listdir(cache_path)]



# 4

def find_password(list_of_files):
for file in list_of_files:
with open(file) as f:
for line in f:
if "password" in line:
split_line = line.split(" ", 1)
return split_line[1].strip()
"""
Here we do the following:
- Loop over every file in a list of files
- Open the file in the iteration
- Loop over every line in the file
- if "password" is found in the line:
- split the line
- return the password and strip off non-ascii characters
"""

# This block is only executed when run from "main", and not when imported.
if __name__ == "__main__":
clean_cache()
cache_zip(data_path, cache_path)
cached_files()
print(find_password(cached_files()))

0 comments on commit fdfd160

Please sign in to comment.