-
Notifications
You must be signed in to change notification settings - Fork 0
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
PR solution #1
base: main
Are you sure you want to change the base?
PR solution #1
Conversation
line = input("Enter content line: ") | ||
if line == "exit": | ||
break | ||
file_data.extend([line]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For one element we use append
method
Where is a new-line symbol at the end of the line?
if "-f" in argv: | ||
index = 0 | ||
|
||
for _ in range(len(argv) - 1): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Python-way for loop
for arg in argv:
index = 0 | ||
|
||
for _ in range(len(argv) - 1): | ||
index += 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid the first element - use slicing in list of arguments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you really need indexes, you can use enumerate
for this
https://docs.python.org/3/library/functions.html#enumerate
|
||
for _ in range(len(argv) - 1): | ||
index += 1 | ||
if argv[_: _ + 1] == ["-f"]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_
- use when the variable need to be skipped!
If you already use index - use your variable index
To check if one of the elements is equal to something - use an element from the cycle instead
filename = "" | ||
directories = [] | ||
|
||
if "-f" in argv: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For different cases we can create different if-else branches
if '-f' in argv:
...
if '-d' in argv:
...
if '-f' in argv and '-d' in argv:
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better to create a function for parsing arguments and after do logic based on parsed args
pass | ||
chdir(directory) | ||
|
||
with open(filename, "w") as f: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
App should add the current timestamp at the top and number lines. If file.txt already exists it should add content below
Open the file with another mode (append)
|
||
for directory in directories: | ||
try: | ||
mkdir(directory) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be relevant to use: os.makedirs
to create directories
|
||
directories = argv[index:len(argv)] | ||
|
||
file_data = [datetime.today().strftime('%y:%d:%m %H-%M-%S'), "\n"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can create a first line using f-string
or str.format
to concate date with \n
symbol
if argv[_: _ + 1] == ["-d"]: | ||
break | ||
|
||
directories = argv[index:len(argv)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can skip len(argv)
due to a slice behaviour
argv[index:len(argv)]
- the same as argv[index:]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You haven't changed the global variable directories
, so it does nothing
It would be best if you changed the logic to modifying list
try: | ||
mkdir(directory) | ||
except Exception: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better if you wrap the error in the correct way
No description provided.