From 90af311a1c4a15c6c23bf4fc7d3c1e5c0698e3b5 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 26 Apr 2026 14:27:17 +0300 Subject: [PATCH 1/2] sol_1 --- app/create_file.py | 80 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/app/create_file.py b/app/create_file.py index fa56336e1..b9e7af3e6 100644 --- a/app/create_file.py +++ b/app/create_file.py @@ -1 +1,79 @@ -# write your code here +import sys +import os +from datetime import datetime +from typing import List + + +def parse_args(args: List[str]) -> tuple[str | None, str | None]: + dir_parts: List[str] = [] + file_name: str | None = None + + i = 1 + while i < len(args): + if args[i] == "-d": + i += 1 + while i < len(args) and not args[i].startswith("-"): + dir_parts.append(args[i]) + i += 1 + continue + + if args[i] == "-f": + i += 1 + if i < len(args): + file_name = args[i] + i += 1 + continue + + i += 1 + + dir_path = os.path.join(*dir_parts) if dir_parts else None + return dir_path, file_name + + +def read_content() -> str: + lines: List[str] = [] + index = 1 + + while True: + line = input("Enter content line: ") + if line == "stop": + break + + lines.append(f"{index} {line}") + index += 1 + + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + + return f"{timestamp}\n" + "\n".join(lines) + "\n" + + +def ensure_directory(dir_path: str | None) -> None: + if dir_path: + os.makedirs(dir_path, exist_ok=True) + + +def write_to_file(file_path: str, content: str) -> None: + if os.path.exists(file_path): + with open(file_path, "a", encoding="utf-8") as file: + file.write("\n" + content) + else: + with open(file_path, "w", encoding="utf-8") as file: + file.write(content) + + +def main() -> None: + dir_path, file_name = parse_args(sys.argv) + + if file_name is None: + return + + ensure_directory(dir_path) + + file_path = os.path.join(dir_path, file_name) if dir_path else file_name + + content = read_content() + write_to_file(file_path, content) + + +if __name__ == "__main__": + main() From 25a46dde41f309e87e8dac4a153381efa3dd9ade Mon Sep 17 00:00:00 2001 From: David Date: Sun, 26 Apr 2026 14:42:40 +0300 Subject: [PATCH 2/2] sol_2 --- app/create_file.py | 85 ++++++++++++++++++---------------------- tests/app/create_file.py | 72 ++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 46 deletions(-) create mode 100644 tests/app/create_file.py diff --git a/app/create_file.py b/app/create_file.py index b9e7af3e6..09d4155d6 100644 --- a/app/create_file.py +++ b/app/create_file.py @@ -1,79 +1,72 @@ import sys import os from datetime import datetime -from typing import List -def parse_args(args: List[str]) -> tuple[str | None, str | None]: - dir_parts: List[str] = [] - file_name: str | None = None +def parse_args(args: list[str]) -> tuple[list[str], str | None]: + directory_parts = [] + file_name = None - i = 1 + i = 0 while i < len(args): if args[i] == "-d": i += 1 - while i < len(args) and not args[i].startswith("-"): - dir_parts.append(args[i]) + while i < len(args) and args[i] != "-f": + directory_parts.append(args[i]) i += 1 - continue - - if args[i] == "-f": + elif args[i] == "-f": i += 1 if i < len(args): file_name = args[i] + i += 1 + else: i += 1 - continue - - i += 1 - dir_path = os.path.join(*dir_parts) if dir_parts else None - return dir_path, file_name + return directory_parts, file_name -def read_content() -> str: - lines: List[str] = [] - index = 1 - +def get_content_from_user() -> list[str]: + lines = [] while True: line = input("Enter content line: ") if line == "stop": break + lines.append(line) + return lines - lines.append(f"{index} {line}") - index += 1 +def build_file_content(lines: list[str]) -> str: timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + content = timestamp + "\n" + for index, line in enumerate(lines, start=1): + content += f"{index} {line}\n" + return content.rstrip("\n") - return f"{timestamp}\n" + "\n".join(lines) + "\n" +def main() -> None: + args = sys.argv[1:] + directory_parts, file_name = parse_args(args) -def ensure_directory(dir_path: str | None) -> None: - if dir_path: + if directory_parts: + dir_path = os.path.join(*directory_parts) os.makedirs(dir_path, exist_ok=True) - - -def write_to_file(file_path: str, content: str) -> None: - if os.path.exists(file_path): - with open(file_path, "a", encoding="utf-8") as file: - file.write("\n" + content) else: - with open(file_path, "w", encoding="utf-8") as file: - file.write(content) - - -def main() -> None: - dir_path, file_name = parse_args(sys.argv) - - if file_name is None: - return - - ensure_directory(dir_path) + dir_path = "" - file_path = os.path.join(dir_path, file_name) if dir_path else file_name + if file_name: + file_path = ( + os.path.join(dir_path, file_name) if dir_path else file_name + ) + lines = get_content_from_user() + content = build_file_content(lines) - content = read_content() - write_to_file(file_path, content) + file_exists = ( + os.path.exists(file_path) and os.path.getsize(file_path) > 0 + ) + with open(file_path, "a") as f: + if file_exists: + f.write("\n\n") + f.write(content) -if __name__ == "__main__": - main() +main() diff --git a/tests/app/create_file.py b/tests/app/create_file.py new file mode 100644 index 000000000..09d4155d6 --- /dev/null +++ b/tests/app/create_file.py @@ -0,0 +1,72 @@ +import sys +import os +from datetime import datetime + + +def parse_args(args: list[str]) -> tuple[list[str], str | None]: + directory_parts = [] + file_name = None + + i = 0 + while i < len(args): + if args[i] == "-d": + i += 1 + while i < len(args) and args[i] != "-f": + directory_parts.append(args[i]) + i += 1 + elif args[i] == "-f": + i += 1 + if i < len(args): + file_name = args[i] + i += 1 + else: + i += 1 + + return directory_parts, file_name + + +def get_content_from_user() -> list[str]: + lines = [] + while True: + line = input("Enter content line: ") + if line == "stop": + break + lines.append(line) + return lines + + +def build_file_content(lines: list[str]) -> str: + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + content = timestamp + "\n" + for index, line in enumerate(lines, start=1): + content += f"{index} {line}\n" + return content.rstrip("\n") + + +def main() -> None: + args = sys.argv[1:] + directory_parts, file_name = parse_args(args) + + if directory_parts: + dir_path = os.path.join(*directory_parts) + os.makedirs(dir_path, exist_ok=True) + else: + dir_path = "" + + if file_name: + file_path = ( + os.path.join(dir_path, file_name) if dir_path else file_name + ) + lines = get_content_from_user() + content = build_file_content(lines) + + file_exists = ( + os.path.exists(file_path) and os.path.getsize(file_path) > 0 + ) + with open(file_path, "a") as f: + if file_exists: + f.write("\n\n") + f.write(content) + + +main()