Skip to content

Instantly share code, notes, and snippets.

@Grimthorr
Last active June 27, 2024 14:01
Show Gist options
  • Save Grimthorr/8ea07f43cebeb4156e54 to your computer and use it in GitHub Desktop.
Save Grimthorr/8ea07f43cebeb4156e54 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).
#!/usr/bin/python
import os
# start editable vars #
outputfile = "~/inventory.txt" # file to save the results to
folder = "~/test" # 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.splitext(fn)[0]
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