-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Create an app create_file.py #1755
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?
Changes from 1 commit
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,46 @@ | ||
| # write your code here | ||
| import os | ||
| import sys | ||
| from datetime import datetime | ||
|
|
||
|
|
||
| def main() -> None: | ||
| args = sys.argv[1:] | ||
| current_flag = None | ||
| dirs = [] | ||
| filename = None | ||
| for arg in args: | ||
| if arg in ["-d", "-f"]: | ||
| current_flag = arg | ||
| continue | ||
| if current_flag == "-d": | ||
| dirs.append(arg) | ||
| elif current_flag == "-f": | ||
| filename = arg | ||
|
|
||
| dirpath = "" | ||
| if dirs: | ||
| for directory in dirs: | ||
| dirpath = f"{dirpath}{directory}/" | ||
| if not os.path.exists(dirpath): | ||
| os.mkdir(dirpath) | ||
|
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 checklist requires using |
||
|
|
||
| if not filename: | ||
| return | ||
|
|
||
| filepath = f"{dirpath}{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. The checklist specifies using |
||
| with open(filepath, "a+") as file: | ||
| has_content = os.path.getsize(filepath) > 0 | ||
| if has_content: | ||
| file.write("\n") | ||
|
|
||
| file.write(datetime.now().strftime("%Y-%m-%d %H:%M:%S\n")) | ||
| index = 0 | ||
| while True: | ||
| content = input("Enter content line: ") | ||
| if content == "stop": | ||
| break | ||
| index += 1 | ||
| file.write(f"{index} {content}\n") | ||
|
|
||
|
|
||
| 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.
This violates the cross-platform requirement from checklist item #2. Use
os.path.join()instead of string concatenation with/. The correct approach is to build the full path first, then create it: