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

args = sys.argv[1:]
dir_path = []
file_name = None

if "-d" in args:
d_idx = args.index("-d")
for i in range(d_idx + 1, len(args)):
if args[i] == "-f":
break
dir_path.append(args[i])

if "-f" in args:
f_idx = args.index("-f")
if f_idx + 1 < len(args):
file_name = args[f_idx + 1]

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

if file_name:
content_lines = []
while True:
line = input("Enter content line: ")
if line.lower() == "stop":
break
content_lines.append(line)

file_path = os.path.join(full_path, file_name)
file_exists = os.path.exists(file_path)

with open(file_path, "a") as f:
if file_exists:
f.write("\n")

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

for index, line in enumerate(content_lines, start=1):
f.write(f"{index} {line}\n")
Comment on lines +1 to +44
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This violates the DRY principle checklist item. Create separate functions (e.g., parse_arguments(), create_directories(), collect_content(), write_file()) to organize the code logically instead of having all code at module level.

Loading