This recipe shows a simple directory listing program. It can accept multiple command-line arguments specifying filenames. These filenames can include wildcard characters like * (asterisk) and ? (question mark), as is common in OS command shells like bash (Unix) and CMD (Windows). Tested on Windows but should work on Unix too, since it uses no OS-specific functions, or rather, it does use them, but that happens under the hood, within the libraries used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | from __future__ import print_function
'''
file_glob.py
Lists filenames matching one or more wildcard patterns.
Author: Vasudev Ram
Copyright 2016 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
Product store: https://gumroad.com/vasudevram
'''
import sys
import glob
sa = sys.argv
lsa = len(sys.argv)
if lsa < 2:
print("{}: Must give one or more filename wildcard arguments.".
format(sa[0]))
sys.exit(1)
for arg in sa[1:]:
print("Files matching pattern {}:".format(arg))
for filename in glob.glob(arg):
print(filename)
|
More details, and sample output, available here.