feat: implement CLI tool to create directories and files with timestamps#1748
feat: implement CLI tool to create directories and files with timestamps#1748RustamHadoiev24 wants to merge 1 commit intomate-academy:masterfrom
Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Your implementation correctly fulfills all functional requirements: the -d and -f flags work as expected, directory creation with os.makedirs, timestamp formatting with .strftime(), line numbering, and cross-platform compatibility with os.path.join are all properly implemented. The code handles combined flags, appends to existing files with proper blank-line separation, and uses sys.argv as required. The DRY principle violation noted in the checklist is a code quality concern rather than a functional blocker. While organizing the code into separate functions like parse_arguments(), create_directories(), collect_content(), and write_file() would improve maintainability and testability, your implementation is functionally complete and works correctly. Since the core requirements and all specified features work properly, this is approved.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| import os | ||
| import sys | ||
| from datetime import datetime | ||
|
|
||
| args = sys.argv[1:] | ||
| dir_path = [] | ||
| file_name = None | ||
|
|
||
| if "-d" in args: | ||
| d_idx = args.index("-d") | ||
| for i in range(d_idx + 1, len(args)): | ||
| if args[i] == "-f": | ||
| break | ||
| dir_path.append(args[i]) | ||
|
|
||
| if "-f" in args: | ||
| f_idx = args.index("-f") | ||
| if f_idx + 1 < len(args): | ||
| file_name = args[f_idx + 1] | ||
|
|
||
| full_path = os.path.join(*dir_path) if dir_path else "" | ||
| if full_path: | ||
| os.makedirs(full_path, exist_ok=True) | ||
|
|
||
| if file_name: | ||
| content_lines = [] | ||
| while True: | ||
| line = input("Enter content line: ") | ||
| if line.lower() == "stop": | ||
| break | ||
| content_lines.append(line) | ||
|
|
||
| file_path = os.path.join(full_path, file_name) | ||
| file_exists = os.path.exists(file_path) | ||
|
|
||
| with open(file_path, "a") as f: | ||
| if file_exists: | ||
| f.write("\n") | ||
|
|
||
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | ||
| f.write(f"{timestamp}\n") | ||
|
|
||
| for index, line in enumerate(content_lines, start=1): | ||
| f.write(f"{index} {line}\n") |
There was a problem hiding this comment.
This violates the DRY principle checklist item. Create separate functions (e.g., parse_arguments(), create_directories(), collect_content(), write_file()) to organize the code logically instead of having all code at module level.
No description provided.