Created
April 19, 2018 11:25
-
-
Save leninhasda/0414792e7d65f526ce078f7af932b5af to your computer and use it in GitHub Desktop.
Simple python script to count files in directory.
This file contains 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
#!/usr/bin/env python | |
import os, sys | |
def countFilesRecursive(dirname): | |
cnt = 0 | |
for file in os.listdir(dirname): | |
fullpath = os.path.join(dirname, file) | |
if os.path.isdir(fullpath): | |
cnt = cnt + countFilesRecursive(fullpath) | |
elif os.path.isfile(fullpath): | |
cnt = cnt + 1 | |
return cnt | |
def main(): | |
dir_list = [] | |
if len(sys.argv) > 1: | |
dir_list = sys.argv[1:] | |
else: | |
dir_list = ["./"] | |
for dir in dir_list: | |
if os.path.isfile(dir): | |
print("\"{}\" is not a directory!".format(dir)) | |
continue | |
dirname = "current" | |
if dir not in ["./", "."]: | |
dirname = dir | |
print("Total {} files in {} directory".format(countFilesRecursive(dir), dirname)) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Copy the content and save in a file. (ex: count-files.py or just download it)
Now you should be able to run it like this: