-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Solution #1765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Solution #1765
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,111 @@ | ||
| # write your code here | ||
| import sys | ||
| import os | ||
| from datetime import datetime | ||
|
|
||
|
|
||
| def parse_args(argv: None) -> None: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameter type should be |
||
| """ | ||
| Parse sys.argv manually to extract -d and -f flag values. | ||
| Returns (dir_parts, filename) where either can be None. | ||
| """ | ||
| dir_parts = None | ||
| filename = None | ||
|
|
||
| i = 1 | ||
| while i < len(argv): | ||
| if argv[i] == "-d": | ||
| dir_parts = [] | ||
| i += 1 | ||
| while i < len(argv) and not argv[i].startswith("-"): | ||
| dir_parts.append(argv[i]) | ||
| i += 1 | ||
| elif argv[i] == "-f": | ||
| i += 1 | ||
| if i < len(argv): | ||
| filename = argv[i] | ||
| i += 1 | ||
| else: | ||
| print("Error: -f flag requires a filename argument.") | ||
| sys.exit(1) | ||
| else: | ||
| print(f"Error: Unknown flag '{argv[i]}'.") | ||
| sys.exit(1) | ||
|
|
||
| return dir_parts, filename | ||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function returns a string (content block) but the return type annotation says |
||
| def collect_content() -> None: | ||
| """ | ||
| Interactively collect lines from the user until they type 'stop'. | ||
| Returns formatted content block (timestamp + numbered lines). | ||
| """ | ||
| lines = [] | ||
| while True: | ||
| line = input("Enter content line: ") | ||
| if line.lower() == "stop": | ||
| break | ||
| lines.append(line) | ||
|
|
||
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | ||
|
|
||
| block = timestamp + "\n" | ||
| for number, text in enumerate(lines, start=1): | ||
| block += f"{number} {text}\n" | ||
|
|
||
| return block | ||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameter type should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function returns |
||
| def create_directory(dir_parts: None) -> None: | ||
| """Join dir_parts into a path and create it | ||
| (including any missing parents).""" | ||
| path = os.path.join(*dir_parts) | ||
| os.makedirs(path, exist_ok=True) | ||
| print(f"Directory created: {path}") | ||
| return path | ||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameter types should be |
||
| def create_file(directory: None, filename: None) -> None: | ||
| """ | ||
| Collect content from user and write/append it to the file. | ||
| If the file already exists, appends a blank line then the new block. | ||
| """ | ||
| filepath = os.path.join(directory, filename) | ||
|
|
||
| already_exists = os.path.isfile(filepath) | ||
|
|
||
| content_block = collect_content() | ||
|
|
||
| with open(filepath, "a") as f: | ||
| if already_exists: | ||
| f.write("\n") | ||
| f.write(content_block) | ||
|
|
||
| print(f"File saved: {filepath}") | ||
|
|
||
|
|
||
| def main() -> None: | ||
| if len(sys.argv) < 2: | ||
| print("Usage:") | ||
| print(" python create_file.py -d dir1 dir2") | ||
| print(" python create_file.py -f file.txt") | ||
| print(" python create_file.py -d dir1 dir2 -f file.txt") | ||
| sys.exit(1) | ||
|
|
||
| dir_parts, filename = parse_args(sys.argv) | ||
|
|
||
| if dir_parts and filename: | ||
| directory = create_directory(dir_parts) | ||
| create_file(directory, filename) | ||
|
|
||
| elif dir_parts: | ||
| create_directory(dir_parts) | ||
|
|
||
| elif filename: | ||
| create_file(".", filename) | ||
|
|
||
| else: | ||
| print("Error: provide at least one flag: -d or -f") | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function returns a tuple
(dir_parts, filename)but the return type annotation says-> None. Should be-> tuple[list | None, str | None]