From 40fdc98cf76c30d13b42215832d40e1f262cf42b Mon Sep 17 00:00:00 2001 From: Adel Pereira Date: Tue, 5 May 2026 11:33:56 +0100 Subject: [PATCH 1/7] Solution --- app/create_file.py | 77 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/app/create_file.py b/app/create_file.py index fa56336e1..0b7798e0f 100644 --- a/app/create_file.py +++ b/app/create_file.py @@ -1 +1,76 @@ -# write your code here +import sys +import os +from datetime import datetime + + +def parse_arguments( + args: list[str] +) -> tuple[list, str]: + directories = [] + add_dir = False + for index, element in enumerate(args): + if element == "-f": + filename = args[index + 1] + add_dir = False + elif element == "-d": + add_dir = True + elif add_dir: + directories.append(element) + return directories, filename + + +def create_directories( + directories: list[str] +) -> str: + # Use os.path.join to create path + # Use os.makedirs with exist_ok=True + path = os.path.join(*directories) + os.makedirs(path, exist_ok=True) + return path + + +def get_content_from_user( +) -> list[str]: + lines = [] + while True: + line = input("Enter content line: ") + if line.lower() == "stop": + break + lines.append(line) + return lines + + +def format_content( + lines: list[str] +) -> str: + current_date = datetime.now() + format_lines = [current_date.strftime("%Y-%m-%d %H:%M:%S")] + for line_no, line in enumerate(lines): + format_lines.append(" ".join([str(line_no + 1), line])) + content = "\n".join(format_lines) + return content + + +def write_to_file( + filepath: str, + content: str +) -> None: + with open(filepath, "a") as f: + f.write(content) + + +def main( +) -> None: + directories, filename = parse_arguments(sys.argv) + if any(directories): + path = create_directories(directories) + filepath = os.path.join(path, filename) + else: + filepath = filename + lines = get_content_from_user() + content = format_content(lines) + write_to_file(filepath, content) + + +if __name__ == "__main__": + main() From b880837d904d63119366b259cfaa6e2563549778 Mon Sep 17 00:00:00 2001 From: Adel Pereira Date: Tue, 5 May 2026 11:35:15 +0100 Subject: [PATCH 2/7] Solution --- app/create_file.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/create_file.py b/app/create_file.py index 0b7798e0f..dd4e65c56 100644 --- a/app/create_file.py +++ b/app/create_file.py @@ -22,8 +22,6 @@ def parse_arguments( def create_directories( directories: list[str] ) -> str: - # Use os.path.join to create path - # Use os.makedirs with exist_ok=True path = os.path.join(*directories) os.makedirs(path, exist_ok=True) return path From 8e32d2ecfc849b7c00aa6dc3d88ed66a868884e9 Mon Sep 17 00:00:00 2001 From: Adel Pereira Date: Tue, 5 May 2026 11:45:11 +0100 Subject: [PATCH 3/7] Solution --- app/create_file.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/create_file.py b/app/create_file.py index dd4e65c56..2065f3bd1 100644 --- a/app/create_file.py +++ b/app/create_file.py @@ -6,11 +6,13 @@ def parse_arguments( args: list[str] ) -> tuple[list, str]: + filename = None directories = [] add_dir = False for index, element in enumerate(args): if element == "-f": - filename = args[index + 1] + if index + 1 < len(args): + filename = args[index + 1] add_dir = False elif element == "-d": add_dir = True @@ -53,13 +55,21 @@ def write_to_file( filepath: str, content: str ) -> None: - with open(filepath, "a") as f: - f.write(content) + if not content.endswith("\n"): + content += "\n" + + if os.path.exists(filepath) and os.path.getsize(filepath) > 0: + content = "\n" + content + + with open(filepath, "a") as output_file: + output_file.write(content) def main( ) -> None: directories, filename = parse_arguments(sys.argv) + if filename is None: + filename = "file.txt" if any(directories): path = create_directories(directories) filepath = os.path.join(path, filename) From d6dad7c53b9c966d9b7a3ab8c4c4d179c338a94e Mon Sep 17 00:00:00 2001 From: Adel Pereira Date: Tue, 5 May 2026 11:46:39 +0100 Subject: [PATCH 4/7] Solution --- app/create_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/create_file.py b/app/create_file.py index 2065f3bd1..d6a7b16b8 100644 --- a/app/create_file.py +++ b/app/create_file.py @@ -5,7 +5,7 @@ def parse_arguments( args: list[str] -) -> tuple[list, str]: +) -> tuple[list[str], str]: filename = None directories = [] add_dir = False From dd0d9ca689199f7eb51caea312c8258622fb0746 Mon Sep 17 00:00:00 2001 From: Adel Pereira Date: Tue, 5 May 2026 12:00:14 +0100 Subject: [PATCH 5/7] Solution --- app/create_file.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/create_file.py b/app/create_file.py index d6a7b16b8..eb5e1589b 100644 --- a/app/create_file.py +++ b/app/create_file.py @@ -68,13 +68,14 @@ def write_to_file( def main( ) -> None: directories, filename = parse_arguments(sys.argv) - if filename is None: - filename = "file.txt" if any(directories): path = create_directories(directories) - filepath = os.path.join(path, filename) - else: + if filename is None: + return + if path is None: filepath = filename + else: + filepath = os.path.join(path, filename) lines = get_content_from_user() content = format_content(lines) write_to_file(filepath, content) From 619f081550129a11d59a9605e010b5a5dfa20801 Mon Sep 17 00:00:00 2001 From: Adel Pereira Date: Tue, 5 May 2026 14:36:37 +0100 Subject: [PATCH 6/7] Solution --- app/create_file.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/create_file.py b/app/create_file.py index eb5e1589b..316ada435 100644 --- a/app/create_file.py +++ b/app/create_file.py @@ -45,8 +45,7 @@ def format_content( ) -> str: current_date = datetime.now() format_lines = [current_date.strftime("%Y-%m-%d %H:%M:%S")] - for line_no, line in enumerate(lines): - format_lines.append(" ".join([str(line_no + 1), line])) + format_lines.extend(f"{i + 1} {line}" for i, line in enumerate(lines)) content = "\n".join(format_lines) return content @@ -55,10 +54,7 @@ def write_to_file( filepath: str, content: str ) -> None: - if not content.endswith("\n"): - content += "\n" - - if os.path.exists(filepath) and os.path.getsize(filepath) > 0: + if os.path.exists(filepath): content = "\n" + content with open(filepath, "a") as output_file: @@ -68,6 +64,7 @@ def write_to_file( def main( ) -> None: directories, filename = parse_arguments(sys.argv) + path = None if any(directories): path = create_directories(directories) if filename is None: From 2a3b68f2663a17d35ffff3ebb1afbb0e8cd2f707 Mon Sep 17 00:00:00 2001 From: Adel Pereira Date: Tue, 5 May 2026 15:14:17 +0100 Subject: [PATCH 7/7] Solution --- app/create_file.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/create_file.py b/app/create_file.py index 316ada435..dc8b6dd67 100644 --- a/app/create_file.py +++ b/app/create_file.py @@ -55,7 +55,7 @@ def write_to_file( content: str ) -> None: if os.path.exists(filepath): - content = "\n" + content + content = "\n\n" + content with open(filepath, "a") as output_file: output_file.write(content) @@ -78,5 +78,4 @@ def main( write_to_file(filepath, content) -if __name__ == "__main__": - main() +main()