Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
57 changes: 56 additions & 1 deletion app/create_file.py
Original file line number Diff line number Diff line change
@@ -1 +1,56 @@
# write your code here
from datetime import datetime
from sys import argv
from os import mkdir, chdir
Copy link
Owner Author

Choose a reason for hiding this comment

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

Suggest using absolute import of basic modules for better readability.
sys.argv more informative than simple argv, that can be simple named variable


filename = ""
directories = []

if "-f" in argv:
index = 0
Copy link
Owner Author

Choose a reason for hiding this comment

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

index isn't informative name for variable, future you will not understand what is it. Care about it)


for _ in range(len(argv) - 1):
Copy link
Owner Author

Choose a reason for hiding this comment

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

too hard reading, try give name of variable, change hard to read construction for _ in range(len(argv) - 1):

index += 1
if argv[_: _ + 1] == ["-f"]:
Copy link
Owner Author

Choose a reason for hiding this comment

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

better way to find index of -f would be to use standard list method such as .index()

break

filename = argv[index]

if "-d" in argv:
index2 = 0

for _ in range(len(argv) - 1):
index2 += 1
if argv[_: _ + 1] == ["-d"]:
Copy link
Owner Author

Choose a reason for hiding this comment

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

The same about complexity of reading and variable naming

break

directories = argv[index2:index - 1]
Copy link
Owner Author

Choose a reason for hiding this comment

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

can be simplified after refactoring of names, and would be great to set a comment, why you use -1

else:
Copy link
Owner Author

Choose a reason for hiding this comment

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

unnecessary else, use elif -d in argv, for simply note that only one param was enterd by console

filename = "file.txt"
Copy link
Owner Author

Choose a reason for hiding this comment

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

failed setting filename for file, because -f argument wasn't pulled


if "-d" in argv:
index = 0
Copy link
Owner Author

Choose a reason for hiding this comment

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

naming as previous examples


for _ in range(len(argv) - 1):
index += 1
if argv[_: _ + 1] == ["-d"]:
Copy link
Owner Author

Choose a reason for hiding this comment

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

use built-in methods for find position of argument in list

break

directories = argv[index:len(argv)]
Copy link
Owner Author

Choose a reason for hiding this comment

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

no sense to use second arg -> len(argv), construction your_list[index:] will return slice from index to the end of list


file_data = [datetime.today().strftime('%y:%d:%m %H-%M-%S'), "\n"]

while True:
line = input("Enter content line: ")
if line == "exit":
Copy link
Owner Author

Choose a reason for hiding this comment

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

on task was keyword "stop", not "exit"

break
file_data.extend([line])
Copy link
Owner Author

Choose a reason for hiding this comment

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

for one line it's better to use .append() method, read about difference of extend and append methods of list


for directory in directories:
Copy link
Owner Author

Choose a reason for hiding this comment

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

try to change that part for using os.makedirs(), instead of creating dir one by one

try:
mkdir(directory)
except Exception:
Copy link
Owner Author

Choose a reason for hiding this comment

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

Maybe we can exception particular errors? Try to test your program, and except errors that you will have!

pass
chdir(directory)

with open(filename, "w") as f:
Copy link
Owner Author

Choose a reason for hiding this comment

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

it must create file only when -f was passed into arguments, not every time of running script.
Also, you 'a' mode for opening file, for appending text! For now, you every time rewrite file, so old file info would be deleted on new write.

f.writelines(file_data)
Copy link
Owner Author

Choose a reason for hiding this comment

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

Care about a new line ('\n') at the end of your file_data, to beautify output file with new lines. Or just iterate through file_data and do every line write