Skip to content

Instantly share code, notes, and snippets.

@bridgeythegeek
Last active March 22, 2020 15:05
Show Gist options
  • Save bridgeythegeek/2febe1f4fb661881cedc774fdd8af864 to your computer and use it in GitHub Desktop.
Save bridgeythegeek/2febe1f4fb661881cedc774fdd8af864 to your computer and use it in GitHub Desktop.
python template for working with files
import argparse
import glob
import logging
import os
log = logging.getLogger(__name__)
# Gather the arguments
argp = argparse.ArgumentParser()
argp.add_argument('-v', '--verbose', action='store_const', dest='loglevel', const=logging.INFO, default=logging.WARNING)
argp.add_argument('-d', '--debug', action='store_const', dest='loglevel', const=logging.DEBUG)
argp.add_argument('-R', '--recursive', action='store_true')
argp.add_argument('target', nargs='+')
args = argp.parse_args()
# Configure logging
logging.basicConfig(format='[%(asctime)s] (%(levelname)-8s) %(message)s', level=args.loglevel, datefmt='%Y-%m-%d %H:%M:%S')
# Gather the files
todos = []
for infile in args.target:
for globbed in glob.glob(infile):
log.debug(f'Globbed: {globbed}')
if os.path.isfile(globbed):
todos.append(globbed)
elif args.recursive:
for dirpath, dirnames, filenames in os.walk(globbed):
for filename in filenames:
todos.append(os.path.join(dirpath, filename))
todos = set(todos)
# Report the files
if args.loglevel < logging.WARNING:
c_len = len(str(len(todos)))
log.info(f"{len(todos)} todos")
if args.loglevel < logging.INFO:
for counter, todo in enumerate(todos):
log.debug(f"{counter+1:>{c_len}} {todo}")
del c_len
# Iterate over the files
for counter, todo in enumerate(todos):
log.info(f"Processing file {todo!r} [{counter+1}/{len(todos)}]")
print(f"file:{todo!r}, size:{os.stat(todo).st_size:,}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment