Skip to content
Open

solved #1766

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

directories = None
filename = None

if "-d" in sys.argv:
d_index = sys.argv.index("-d")
f_index = sys.argv.index("-f") if "-f" in sys.argv else len(sys.argv)
end = f_index if f_index > d_index else len(sys.argv)
directories = sys.argv[d_index + 1 : end]

if "-f" in sys.argv:
filename = sys.argv[sys.argv.index("-f") + 1]

if directories:
path = os.path.join(*directories)
os.makedirs(path, exist_ok=True)
if filename:
filename = os.path.join(path, filename)

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

timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
file_exists = os.path.exists(filename)

with open(filename, "a") as file:
if file_exists:
file.write("\n")
file.write(timestamp + "\n")
for number, line in enumerate(lines, start=1):
file.write(f"{number} {line}\n")
Comment on lines +1 to +39
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 #3 violation: All code is at module level without separate functions. The task requires creating separate functions to avoid repeating blocks of code. Consider refactoring the logic into functions like parse_arguments(), create_directories(), and write_file_content() to follow the DRY principle and improve code organization.

Loading