Created
June 24, 2024 09:15
-
-
Save yeiichi/cb84b10ba159115c5952855e43922539 to your computer and use it in GitHub Desktop.
Guess the MIME types of the files in the file directory.
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 mimetypes | |
from pathlib import Path | |
CWD = Path('.') | |
def file_mime_typer(fp): | |
"""Guess the MIME type of file. | |
Returns (dict): | |
MIME type, filename, and binary content of the file. | |
References: | |
https://docs.python.org/3/library/email.examples.html#id2 | |
""" | |
# MIME type | |
ctype, encoding = mimetypes.guess_type(fp) | |
if (ctype is None) or (encoding is not None): | |
ctype = 'application/octet-stream' # Miscellaneous types | |
maintype, subtype = ctype.split('/', maxsplit=1) | |
# Binary content | |
with open(fp, 'rb') as f: | |
bin_content = f.read() | |
return dict(maintype=maintype, subtype=subtype, filename=fp.name, bin_data=bin_content) | |
def dir_mime_typer(file_dir): | |
"""Guess the MIME types of the files in the file directory. | |
Args: | |
file_dir (path_-like): | |
path-like to the directory containing the target files. | |
Yields: | |
MIME types, filenames, and sizes of the files. | |
""" | |
for path_ in Path(file_dir).glob('*'): | |
if not path_.is_file(): | |
continue | |
yield file_mime_typer(path_) | |
if __name__ == '__main__': | |
mime_type_dicts = dir_mime_typer(input('File DIR? >> ')) | |
print() | |
for d in mime_type_dicts: | |
fname = d.get('filename') | |
mtype = d.get('maintype') | |
stype = d.get('subtype') | |
data_ = d.get('bin_data') | |
print(f'''\ | |
filename: {fname} | |
maintype: {mtype} | |
subtype : {stype} | |
filesize: {len(data_)} | |
''' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment