Anybody need a clean up script?

0 Halcyon Abraham Ramirez · September 12, 2015
Usage of this sweet and simple script I wrote is pretty simple:
give it an absolute path and a file extension and it'll delete all those file extensions in that path.
Disclaimer. I will not be held liable if you delete an important file with this script.
I have so much junk .txt, .xlsx and other files that I decided to wipe them all out in one go.
so if you have an important file on your specified path. put it somewhere safe.



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"):
            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

 
example usage:



path = r"C:\Users\YoMomma\Desktop"

a = CleanUp(path)
a.del_items(".txt")


Post a Reply

Replies

Oldest  Newest  Rating
+2 Halcyon Abraham Ramirez · September 13, 2015
@sfolje 0
tnx for trying? that's some condescending s**t right there

what a dick.
  • 1

Python

107,092 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator