Add makedirs to file operations (#3289)

* Add makedirs to file operations

* Add new directory tests for file operations

* Fix wrong setUp test error

* Simplify makedirs and use correct nested path

* Fix linter error

---------

Co-authored-by: Nicholas Tindle <nick@ntindle.com>
Co-authored-by: James Collins <collijk@uw.edu>
This commit is contained in:
Montana Flynn
2023-04-27 23:12:24 +07:00
committed by GitHub
parent 9e17a304de
commit 7cd76b8d8e
2 changed files with 16 additions and 10 deletions

View File

@@ -142,8 +142,7 @@ def write_to_file(filename: str, text: str) -> str:
return "Error: File has already been updated."
try:
directory = os.path.dirname(filename)
if not os.path.exists(directory):
os.makedirs(directory)
os.makedirs(directory, exist_ok=True)
with open(filename, "w", encoding="utf-8") as f:
f.write(text)
log_operation("write", filename)
@@ -167,6 +166,8 @@ def append_to_file(filename: str, text: str, should_log: bool = True) -> str:
str: A message indicating success or failure
"""
try:
directory = os.path.dirname(filename)
os.makedirs(directory, exist_ok=True)
with open(filename, "a") as f:
f.write(text)
@@ -236,6 +237,8 @@ def download_file(url, filename):
filename (str): Filename to save the file as
"""
try:
directory = os.path.dirname(filename)
os.makedirs(directory, exist_ok=True)
message = f"{Fore.YELLOW}Downloading file from {Back.LIGHTBLUE_EX}{url}{Back.RESET}{Fore.RESET}"
with Spinner(message) as spinner:
session = requests.Session()