-
Notifications
You must be signed in to change notification settings - Fork 1.6k
implement create_file #1768
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?
implement create_file #1768
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,75 @@ | ||
| # write your code here | ||
| import sys | ||
| import os | ||
| import datetime as dt | ||
|
|
||
|
|
||
| def create_directories(directories: list) -> str: | ||
| dir_path = "" | ||
| for directory in directories: | ||
| dir_path = os.path.join(dir_path, directory) | ||
| if dir_path and not os.path.exists(dir_path): | ||
| os.makedirs(dir_path) | ||
| return dir_path | ||
|
|
||
|
|
||
| if "-f" not in sys.argv and "-d" not in sys.argv: | ||
| print("Usage: python create_file.py -f <filename> or -d <directory>") | ||
| sys.exit(1) | ||
|
|
||
| file_path = "" | ||
| directories = [] | ||
|
|
||
| d_flag_index = sys.argv.index("-d") if "-d" in sys.argv else None | ||
| f_flag_index = sys.argv.index("-f") if "-f" in sys.argv else None | ||
|
|
||
| if d_flag_index is not None: | ||
| last_dir_index = len(sys.argv) - 1 | ||
|
|
||
| if d_flag_index + 1 >= len(sys.argv): | ||
| print("Error: '-d' flag provided but no directory specified.") | ||
| sys.exit(1) | ||
|
|
||
| for directory in sys.argv[d_flag_index + 1:]: | ||
| if directory.startswith("-"): | ||
| break | ||
| directories.append(directory) | ||
|
|
||
| file_path = create_directories(directories) | ||
|
Comment on lines
+25
to
+37
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 validation on line 27-32 only checks if the first argument after |
||
|
|
||
| if f_flag_index is not None: | ||
| if f_flag_index + 1 >= len(sys.argv): | ||
| print("Error: '-f' flag provided but no filename specified.") | ||
| sys.exit(1) | ||
| elif f_flag_index + 1 == d_flag_index: | ||
| print( | ||
| "Error: '-f' flag provided but no filename specified " | ||
| "(next argument is '-d')." | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| filename = sys.argv[f_flag_index + 1] | ||
|
|
||
| if os.path.isdir(filename): | ||
| print("Error: Specified filename is a directory.") | ||
| sys.exit(1) | ||
|
|
||
| file_path = os.path.join(file_path, filename) if file_path else filename | ||
|
|
||
| is_append_mode = os.path.exists(file_path) | ||
|
|
||
| with open(file_path, "a") as file: | ||
| time_stamp = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | ||
| if is_append_mode: | ||
| file.write("\n") | ||
| file.write(f"{time_stamp}\n") | ||
|
|
||
| token = "" | ||
| counter = 1 | ||
|
|
||
| while True: | ||
| token = input("Enter content line: ") | ||
| if token == "stop": | ||
| # file.write("\n") | ||
| break | ||
| file.write(f"{counter} {token}\n") | ||
|
Comment on lines
+71
to
+74
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. Checklist violation: This |
||
| counter += 1 | ||
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.
The
-dvalidation doesn't properly handle when directories list is empty (e.g.,python create_file.py -d -f file.txt). The checkd_flag_index + 1 == f_flag_indexcatches this case with an error message, which is good. However, this error handling is redundant since the main check at line 29d_flag_index + 1 >= len(sys.argv)should catch it first - but it doesn't when-fexists right after-d. The current logic works but could be simplified.