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

from datetime import datetime


args = sys.argv[1:]
dirs = []
filename = None

mode = None
for arg in args:
if arg == "-d":
mode = "d"
elif arg == "-f":
mode = "f"
else:
if mode == "d":
dirs.append(arg)
elif mode == "f":
filename = arg
mode = None

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

if filename:
lines = []
while True:
line = input("Enter content line: ")
if line == "stop":
break
lines.append(line)

file_path = os.path.join(dir_path, filename) if dir_path else filename

needs_blank_line = (
os.path.exists(file_path) and os.path.getsize(file_path) > 0
)

with open(file_path, "a") as f:

if needs_blank_line:
f.write("\n")

timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
f.write(f"{timestamp}\n")

for i, line_content in enumerate(lines, 1):
f.write(f"{i} {line_content}\n")
Comment on lines +42 to +52
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 item #2 violation: The file writing logic (timestamp writing, line numbering, blank line handling) is repeated inline and not extracted into a separate function. Consider creating a write_content_to_file function to avoid code repetition.

Loading