Created
March 30, 2021 23:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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