Created
July 13, 2018 06:20
-
-
Save stevensdotb/7767c3d210b8e0ce45e548f8b7e421ca to your computer and use it in GitHub Desktop.
Remove files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Removes all files with a given extension | |
in the directory specified in the path. | |
Arguments in command line: | |
path: Path directory of the files | |
ext: The extension of the files | |
""" | |
import os | |
import sys | |
import argparse | |
class Remove: | |
def __init__(self, path, ext): | |
self.path = path | |
self.ext = ext.replace('.','') | |
def remove_files(self): | |
try: | |
files = os.listdir(self.path) | |
count = 0 | |
print("Path files: " + self.path + "\t\tScript: " + sys.argv[0][8:] + "\n") | |
for file in files: | |
ext = file.split('.')[1] | |
if self.ext == ext: | |
os.remove(self.path + "\\" + file) | |
print(" ~ " + file + " - Removed") | |
count += 1 | |
if count == 0: | |
print("No match files with the extension: *." + self.ext) | |
else: | |
print("\n\rTotal files removed: %d" %(count)) | |
sys.exit(1) | |
except Exception as e: | |
print(e) | |
sys.exit(1) | |
def Main(): | |
parse = argparse.ArgumentParser(prog="Remove Files", | |
description="Remove all files with a specific extension") | |
parse.add_argument("path", help="Path directory where the files are located.", type=str) | |
parse.add_argument("ext", help="The extension of the files to remove.", type=str) | |
args = parse.parse_args() | |
remove = Remove(args.path, args.ext) | |
remove.remove_files() | |
if __name__ == '__main__': | |
Main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment