Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion app/create_file.py
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The -d validation doesn't properly handle when directories list is empty (e.g., python create_file.py -d -f file.txt). The check d_flag_index + 1 == f_flag_index catches this case with an error message, which is good. However, this error handling is redundant since the main check at line 29 d_flag_index + 1 >= len(sys.argv) should catch it first - but it doesn't when -f exists right after -d. The current logic works but could be simplified.


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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 -d is a flag. However, when both flags are used (e.g., python create_file.py -d dir1 dir2 -f), arguments between -d and -f are not validated. This causes -f to be included in the directories list, and os.makedirs("-f") will fail. Consider checking all directory arguments to ensure none start with '-' before calling create_directories.


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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checklist violation: This else clause is unreachable. When token == 'stop', the loop breaks, so the else block never executes. Remove the else keyword - the code structure already handles this correctly via the while True loop.

counter += 1
Loading