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

PR solution #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

PR solution #1

wants to merge 1 commit into from

Conversation

andrsj
Copy link
Owner

@andrsj andrsj commented Jan 12, 2023

No description provided.

line = input("Enter content line: ")
if line == "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 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):
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 Python-way for loop
for arg in argv:

index = 0

for _ in range(len(argv) - 1):
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.

To avoid the first element - use slicing in list of arguments

Copy link
Owner Author

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"]:
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 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:
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 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:
    ...

Copy link
Owner Author

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:
Copy link
Owner Author

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)
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 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"]
Copy link
Owner Author

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)]
Copy link
Owner Author

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:]

Copy link
Owner Author

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
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 would be better if you wrap the error in the correct way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants