Skip to content

Instantly share code, notes, and snippets.

@kparms
Forked from Grimthorr/directory-inventory.py
Last active November 18, 2022 01:06
Show Gist options
  • Save kparms/6efe54e19809df9e96ece274c7385d74 to your computer and use it in GitHub Desktop.
Save kparms/6efe54e19809df9e96ece274c7385d74 to your computer and use it in GitHub Desktop.
Python script to generate a text file listing all files from a given directory (including those in sub-folders).
import os
# start editable vars #
outputfile = "inventory.txt" # file to save the results to
folder = "C:\\Users\\[User]\\Documents" # the folder to inventory
exclude = ['Thumbs.db','.tmp'] # exclude files containing these strings
pathsep = "\\" # path seperator ('/' for linux, '\\' for Windows)
# end editable vars #
with open(outputfile, "w") as txtfile:
for path,dirs,files in os.walk(folder):
sep = "\n---------- " + path.split(pathsep)[len(path.split(pathsep))-1] + " ----------"
print sep
#txtfile.write("%s\n" % sep)
for fn in sorted(files):
if not any(x in fn for x in exclude):
filename = os.path.join(path, fn)
print filename
txtfile.write("%s\n" % filename)
txtfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment