Skip to content
Open
Changes from 5 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
63 changes: 62 additions & 1 deletion app/create_file.py
Original file line number Diff line number Diff line change
@@ -1 +1,62 @@
# write your code here
from sys import argv, exit
from os import makedirs, path # , getcwd
from datetime import datetime

# argv = ["app/create_file.py", "-f ", " file.txt"]

if len(argv) <= 2 :
print("Usage: -f <filename> or/and -d <directory>")
exit(0)

filename = ""
directory = []
stop = False
content = []
line_number = 1

del argv[0]

for index in range(len(argv)):
if argv[index] == "-f":
try:
filename = argv[index + 1].strip().lower()
del argv[index: index + 2]
break
except IndexError:
print("Error: correct format -f <filename>")
exit(1)

for index in range(len(argv)):
if argv[index].strip() == "-d":
if index == len(argv) - 1:
print("Error: correct format -d <directory name(s)>")
exit(1)
directory = [name.strip() for name in argv[index + 1:]]
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: This logic is broken when -d comes before -f. The range(len(argv)) is evaluated once at loop start, but argv elements are deleted in the first loop. When -d appears first (e.g., ['-d', 'dir1', '-f', 'file.txt']), after processing -d, the code tries to access indices beyond the shortened list, causing IndexError. Fix: collect -d arguments until another flag is encountered, not all remaining args.


# print(f"Path: {path.join(*directory, filename)}")
# print(f"file: {filename}")

if directory != []:
makedirs(path.join(*directory), exist_ok=True)

if filename != "":
full_path = path.join(*directory, filename)

while not stop:
line = input("Enter content line: ")
if line.lower() == "stop":
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use double quotes consistently as per the checklist requirement.

stop = True
else:
content.append(f"\n{line_number} {line}")
line_number += 1
# content.append("\n")

# current_dir = getcwd()
# print(current_dir)

with open(full_path, "a") as f:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When appending to an existing file, there's no blank line added before the new timestamp. Per the requirements example, there should be a blank line between content batches. Consider checking if file exists and adding separator.

f.write(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
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 timestamp is written without a trailing newline (line 57), but first content line has \n prefix (line 50). This causes the output to be 2022-02-01 14:41:101 Line1 content on the SAME line instead of separate lines. Add newline after timestamp.

for line in content:
f.write(line)
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 #5 VIOLATION: The code doesn't use separate functions for reusable operations. Per the checklist 'Create separate functions in order not to have repeating blocks of code', extract content collection into get_content(), file writing into write_to_file(), and argument parsing into parse_arguments().


# print("Finish")
Loading