Skip to content
Open
Changes from 1 commit
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
47 changes: 46 additions & 1 deletion app/create_file.py
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
# write your code here
import os
import sys
from datetime import datetime


def main() -> None:
args = sys.argv[1:]
current_flag = None
dirs = []
filename = None
for arg in args:
if arg in ["-d", "-f"]:
current_flag = arg
continue
if current_flag == "-d":
dirs.append(arg)
elif current_flag == "-f":
filename = arg

dirpath = ""
if dirs:
for directory in dirs:
dirpath = f"{dirpath}{directory}/"
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 cross-platform requirement from checklist item #2. Use os.path.join() instead of string concatenation with /. The correct approach is to build the full path first, then create it:

if not os.path.exists(dirpath):
os.mkdir(dirpath)
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 checklist requires using os.makedirs() instead of os.mkdir(). The current implementation using os.mkdir() will fail when creating nested directories (e.g., dir1/dir2 where dir1 doesn't exist yet). Replace os.mkdir() with os.makedirs().


if not filename:
return

filepath = f"{dirpath}{filename}"
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 checklist specifies using os methods like os.path.join() for cross-platform compatibility. Using string concatenation with / (e.g., f"{dirpath}{directory}/" and f"{dirpath}{filename}") will not work correctly on Windows where the path separator is \. Replace these with os.path.join().

with open(filepath, "a+") as file:
has_content = os.path.getsize(filepath) > 0
if has_content:
file.write("\n")

file.write(datetime.now().strftime("%Y-%m-%d %H:%M:%S\n"))
index = 0
while True:
content = input("Enter content line: ")
if content == "stop":
break
index += 1
file.write(f"{index} {content}\n")


main()
Loading