I managed to tinker around and figured out a way to fix this issue myself.
This is what my result looks like:
from os import listdir
from os.path import isfile, join
dates = [f for f in listdir(os.path.expanduser("~/Desktop/Sam's Calendar")) if isfile(join(os.path.expanduser("~/Desktop/Sam's Calendar"), f))]
dates = [os.path.splitext(x)[0] for x in dates]
if len(os.listdir(os.path.expanduser("~/Desktop/Sam's Calendar"))) == 0:
print("You currently have no reminders.")
elif len(os.listdir(os.path.expanduser("~/Desktop/Sam's Calendar"))) == 1:
print("The following reminder currently exists:\n")
for c, filename in enumerate(dates, 1):
print((str(c) + "."), filename)
else:
print("The following reminders currently exist:\n")
for c, filename in enumerate(dates, 1):
print((str(c) + "."), filename)
if len(os.listdir(os.path.expanduser("~/Desktop/Sam's Calendar"))) > 0:
deletion=input("\nPlease select the number corresponding to the reminder you want to delete.\n\n")
if deletion:
os.remove(os.path.expanduser("~/Desktop/Sam's Calendar/" + dates[(int(deletion) - 1)] + ".txt"))
Below I will try to go through the changes I made:
1.
if len(os.listdir(os.path.expanduser("~/Desktop/Sam's Calendar"))) == 0:
print("You currently have no reminders.")
I make use of the len() function to tell the user if no files exist within the directory.
2.
else:
if len(os.listdir(os.path.expanduser("~/Desktop/Sam's Calendar"))) == 1:
print("The following reminder currently exists:\n")
for c, filename in enumerate(dates, 1):
print((str(c) + "."), filename)
This one is redundant, really. The only difference between this block and the next are the words "reminder" (instead of the plural reminders) and "exists" (instead of the plural exist)
If one file exists, the program outputs the name of the file in this format:
- examplefile_a
3.
else:
print("The following reminders currently exist:\n")
for c, filename in enumerate(dates, 1):
print((str(c) + "."), filename)
As stated, this block does the same as the previous one, but will run if 2 or more files exist in the directory.
The output looks like this:
- examplefile_a
- examplefile_b
- examplefile_c
4.
if len(os.listdir(os.path.expanduser("~/Desktop/Sam's Calendar"))) > 0:
deletion=input("\nPlease select the number corresponding to the reminder you want to delete.\n\n")
if deletion:
os.remove(os.path.expanduser("~/Desktop/Sam's Calendar/" + dates[(int(deletion) - 1)] + ".txt"))
This is where the magic happens.
If there are more than 0 files, the program asks for user input. The user will input the number on the list that corresponds to the file they wish to delete.
The Program makes "deletion" into an integer and subtracts it by 1, then uses the result to delete the correct file in the directory with this full line, where dates[0] corresponds to examplefile_a and so on:
os.remove(os.path.expanduser("~/Desktop/Sam's Calendar/" + dates[(int(deletion) - 1)] + ".txt"))
The reason I use subtraction is that the list in which the file names are placed starts counting from 0. This means that if the user wants to select examplefile_a (user input is "1"), due to the input, the program would select examplefile_b (the list value is "1"). However, the program now subtracts the user input by 1, so that when the user selects examplefile_a (user input is "1"), the program also selects examplefile_a (list value "0").
I hope the explanation isn't too messy, and that I might help someone out in the future with the same issue.