Skip to content

Instantly share code, notes, and snippets.

@phette23
Created March 30, 2021 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phette23/d338f702d78c9af33bac543b8b84fc90 to your computer and use it in GitHub Desktop.
Save phette23/d338f702d78c9af33bac543b8b84fc90 to your computer and use it in GitHub Desktop.
iterate over list of files & write information about them (including MIME type) to CSV
#!/usr/bin/env python3
import csv
import os
import subprocess
with open('files.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['path', 'size (bytes)', 'time last accessed', 'time last modified', 'time created', 'mime type'])
with open('dbcheck-files.txt', 'r') as listfile:
for path in listfile:
path = path.strip()
stat = os.stat(path)
# python's mimetypes library only guesses based on file extension, not contents
# so seems like UNIX `file` command is much more informative
result = subprocess.run(['file', path], stdout=subprocess.PIPE, universal_newlines=True)
mime = ''.join(result.stdout.split(': ')[1:])
writer.writerow([path, stat.st_size, stat.st_atime, stat.st_mtime, stat.st_ctime, mime])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment