Skip to content
Open

Develop #1770

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


def parse_arguments(
args: list[str]
) -> tuple[list[str], str]:
filename = None
directories = []
add_dir = False
for index, element in enumerate(args):
if element == "-f":
if index + 1 < len(args):
filename = args[index + 1]
add_dir = False
elif element == "-d":
add_dir = True
elif add_dir:
directories.append(element)
Comment on lines +6 to +20
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Critical bug: filename is referenced on line 18 but may never be assigned if -f flag is not provided. This will cause a NameError when only -d is used. Initialize filename to empty string before the loop or add validation.

return directories, filename


def create_directories(
directories: list[str]
) -> str:
path = os.path.join(*directories)
os.makedirs(path, exist_ok=True)
return path


def get_content_from_user(
) -> list[str]:
lines = []
while True:
line = input("Enter content line: ")
if line.lower() == "stop":
break
lines.append(line)
return lines


def format_content(
lines: list[str]
) -> str:
current_date = datetime.now()
format_lines = [current_date.strftime("%Y-%m-%d %H:%M:%S")]
format_lines.extend(f"{i + 1} {line}" for i, line in enumerate(lines))
content = "\n".join(format_lines)
Comment on lines +42 to +49
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 content format adds a blank line between timestamp and first numbered line in format_content, but when appending to an existing file, there should be a blank line BEFORE the new timestamp block. Consider adding a newline at the start of formatted content, or handle the empty file case separately.

return content


def write_to_file(
filepath: str,
content: str
) -> None:
if os.path.exists(filepath):
content = "\n\n" + content

with open(filepath, "a") as output_file:
output_file.write(content)


def main(
) -> None:
Comment on lines +63 to +65
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If only -f is provided (no -d), path is never assigned, causing a NameError on line 75. Initialize path = None at the start of main().

directories, filename = parse_arguments(sys.argv)
path = None
if any(directories):
path = create_directories(directories)
if filename is None:
return
if path is None:
filepath = filename
else:
filepath = os.path.join(path, filename)
lines = get_content_from_user()
content = format_content(lines)
write_to_file(filepath, content)
Comment on lines +63 to +78
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Critical bug: When only -d flag is provided, the script incorrectly creates a file. According to requirements, -d dir1 dir2 should only create directories. The current code defaults filename to 'file.txt' and proceeds to create a file even when -f was never passed. The file creation logic should be conditional on whether -f was explicitly provided.



main()
Loading