Skip to content

Instantly share code, notes, and snippets.

@stevensdotb
Last active July 13, 2018 06:16
Show Gist options
  • Save stevensdotb/674315d0b2906aa14fbe4d9656da27a9 to your computer and use it in GitHub Desktop.
Save stevensdotb/674315d0b2906aa14fbe4d9656da27a9 to your computer and use it in GitHub Desktop.
Files counter
"""
Counts 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 countFiles:
def __init__(self, path, ext):
self.path = path
self.ext = ext.replace('.','')
def counting(self):
try:
files = os.listdir(self.path)
count = 0
print("Path files: " + self.path + "\t\tScript: " + sys.argv[0][:] + "\n")
for file in files:
ext = file.split('.')[1]
if self.ext == ext:
print(" ~ " + file)
count += 1
if count == 0:
print("No match files with the extension: *." + self.ext)
else:
print("\n\rTotal files: %d\t\tExtension: *.%s" %(count, self.ext))
sys.exit(1)
except Exception as e:
print(e)
sys.exit(1)
def Main():
parse = argparse.ArgumentParser(prog="File Counter",
description="Counts 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.", type=str)
args = parse.parse_args()
count_files = countFiles(args.path, args.ext)
count_files.counting()
if __name__ == '__main__':
Main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment