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
51 changes: 50 additions & 1 deletion app/create_file.py
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
# write your code here
import sys
import os
import datetime


data = sys.argv


def write_file(filepath: str) -> None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code structure issue: The write_file() function should handle appending to existing files separately from creating new files. Consider passing additional parameters or checking file existence before deciding write mode.

if os.path.exists(filepath):
with open(filepath, "a") as file:
file.write("\n")
file.write(datetime.datetime.now().strftime("%Y-%m-%d "
"%H:%M:%S") + "\n")
line_number = 1
while True:
text = input("Enter content line: ")
if text == "stop":
break
file.write(f"{line_number} {text}\n")
line_number += 1
else:
with open(filepath, "w") as file:
file.write(datetime.datetime.now().strftime("%Y-%m-%d "
"%H:%M:%S") + "\n")
line_number = 1
while True:
text = input("Enter content line: ")
if text == "stop":
break
file.write(f"{line_number} {text}\n")
line_number += 1


if "-d" in data:
dirs = []
for item in data[data.index("-d") + 1:]:
if item in "-f":
Comment on lines +37 to +38
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: This uses in for substring checking instead of == for exact match. When the loop encounters -d, it breaks prematurely because -d is a single character contained in the string "-f". Should be if item == "-f": to only stop when reaching the exact -f flag.

break
dirs.append(item)

path = os.path.join(*dirs) if dirs else "."
os.makedirs(path, exist_ok=True)

if "-f" in data:
filename = str(data[data.index("-f") + 1])
write_file(os.path.join(path, filename))

elif "-f" in data:
write_file(str(data[data.index("-f") + 1]))
Loading