-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Develop #1770
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?
Develop #1770
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,81 @@ | ||
| # write your code here | ||
| import sys | ||
| import os | ||
| from datetime import datetime | ||
|
|
||
|
|
||
| def parse_arguments( | ||
| args: list[str] | ||
| ) -> tuple[list[str], str]: | ||
| filename = None | ||
| directories = [] | ||
| add_dir = False | ||
| for index, element in enumerate(args): | ||
| if element == "-f": | ||
| if index + 1 < len(args): | ||
| 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: | ||
| 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")] | ||
| format_lines.extend(f"{i + 1} {line}" for i, line in enumerate(lines)) | ||
| content = "\n".join(format_lines) | ||
|
Comment on lines
+42
to
+49
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. The content format adds a blank line between timestamp and first numbered line in |
||
| return content | ||
|
|
||
|
|
||
| def write_to_file( | ||
| filepath: str, | ||
| content: str | ||
| ) -> None: | ||
| if os.path.exists(filepath): | ||
| content = "\n\n" + content | ||
|
|
||
| with open(filepath, "a") as output_file: | ||
| output_file.write(content) | ||
|
|
||
|
|
||
| def main( | ||
| ) -> None: | ||
|
Comment on lines
+63
to
+65
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. If only |
||
| directories, filename = parse_arguments(sys.argv) | ||
| path = None | ||
| if any(directories): | ||
| path = create_directories(directories) | ||
| 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) | ||
|
Comment on lines
+63
to
+78
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. Critical bug: When only |
||
|
|
||
|
|
||
| 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.
Critical bug:
filenameis referenced on line 18 but may never be assigned if-fflag is not provided. This will cause a NameError when only-dis used. Initializefilenameto empty string before the loop or add validation.