#!/usr/bin/env python3 import sys import os def stat(path): # use list comprehension to ignore files begining with . files = [os.path.join(path,f) for f in os.listdir(path) if not f.startswith('.')] # Sort our files in ascending order files.sort() for f in files: # print(f, s) if os.path.isdir(f): print(f) stat(f) elif os.path.isfile(f): s = os.stat(f) print('File: ', f, str(s.st_size)+'b') # Allow the user to specify a directory to list the contents of if __name__ == '__main__': path = '.' # Handle arguments if len(sys.argv) > 1: # The path should be the only argument not starting with a - for a in sys.argv[1:]: if not a.startswith('-'): path = a # If the directory does not exist quit if not os.path.isdir(path): print('Directory does not exist.') exit() # Break since we should have found the path break stat(path) # print(' '.join(files))