-1

I am trying to allow the user to input a number to select a file from the displayed list.

This is what the code block 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]
print("The following reminders currently exist:\n")
for c, filename in enumerate(dates, 1):
    print((str(c) + "."), filename)
deletion = input ("\nPlease select the number corresponding to the reminder you want to delete.\n\n")

When I run the code, I get this result:

The following reminders currently exist:

  1. 2019-05-01
  2. 2019-05-02

Please select the number corresponding to the reminder you want to delete.

The results are dynamic. There is effectively no limit to the number of files within the directory. As such, I need a solution that doesn't require me to specify the file names in the code, but one that is also dynamic.

How do I go about letting the user input a number (in this case 1 or 2) to select the file for deletion? I would also like to have an error if they input 0 or anything above the max (which in this case, again, is 2).

Community
  • 1
  • 1
KungenSam
  • 65
  • 1
  • 6
  • Possible duplicate of https://stackoverflow.com/questions/19964603/creating-a-menu-in-python – AutoTester213 Feb 27 '19 at 13:09
  • Possible duplicate of [Creating a Menu in Python](https://stackoverflow.com/questions/19964603/creating-a-menu-in-python) – tripleee Feb 27 '19 at 13:10
  • I think that would work, except the files in my list aren't fixed, and there is no upper limit to the number of files. Perhaps you could make that list dynamic but I wouldn't know how. – KungenSam Feb 27 '19 at 13:29

1 Answers1

0

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:

  1. 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:

  1. examplefile_a
  2. examplefile_b
  3. 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.

KungenSam
  • 65
  • 1
  • 6