0

I have this code:

class CleanUp:
    def __init__(self,directory):
        self.directory = directory

    def del_items(self,*file_extensions):
        """deletes specified file extensions in specificied directory"""

        removed_files = [file for file in os.listdir(self.directory) for ext in file_extensions if ext in file]
        for index ,file in enumerate(removed_files):
            print(str(index + 1) + ": " + file + "\n")
        confirm_delete = input("are you sure you want to delete all {0} files? y|n ".format(len(removed_files)))
        while confirm_delete.lower() not in ("y","n"):<--------- this while loop
            confirm_delete = input("are you sure you want to delete all {0} files? y|n ".format(len(removed_files)))
        if confirm_delete.lower() == "y":
            for file in removed_files:
                try:
                    os.remove(os.path.join(self.directory,file))
                except:
                    pass
            print("successfully deleted {0} files".format(len(removed_files)))
        else:
            print("deletion cancelled goodbye")
            pass




directory = input("please enter a directory ")
while not os.path.exists(directory):
    print("{0} is not a valid directory \n".format(directory))
    directory = input("please enter a directory ")

file_extensions = input("please put in file extensions of files that need deleting. seperate them by one space ")
file_extensions = file_extensions.split()
desktop = CleanUp(directory)
deleted_files = desktop.del_items(*file_extensions)

This line works

while confirm_delete.lower() not in ("y","n"):

however, when I try to do

while confirm_delete.lower() != "y" or confirm_delete.lower() != "n":

the while loop never passes. I'm sure it has something to do with the or but why doesn't it work when done like that?

3
  • De Morgan's laws strike again! :) Commented Aug 29, 2015 at 8:31
  • @De Morgan's laws what you mean? :/ Commented Aug 29, 2015 at 8:35
  • I get it now. well here is a cute emoji for you t(','t) Commented Aug 29, 2015 at 8:42

1 Answer 1

3

Because that condition will always be true; there is no string value which is both "y" and "n" at the same time. Use and instead.

Sign up to request clarification or add additional context in comments.

1 Comment

right! it's just well you know. goddarnit that confuses me sometimes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.