Aug-21-2019, 03:39 AM
there are a number of different functions and methods in a variety of different modules to return a status of whether a named file is of a particular type or not. is there a function that just returns what type the file is?
(Aug-21-2019, 03:39 AM)Skaperen Wrote: [ -> ]there are a number of different functions and methods in a variety of different modules to return a status of whether a named file is of a particular type or not. is there a function that just returns what type the file is?
.py
which the other i mention can not.tom@tom:~/Documents/py_files$ ptpython >>> import magic >>> magic.from_file('test.html') 'HTML document, ASCII text' >>> magic.from_file('toss.py') 'Python script, ASCII text executable' >>> magic.from_file('geckodriver-v0.24.0-linux64.tar.gz') 'gzip compressed data, last modified: Mon Jan 28 22:49:19 2019, from Unix' # Rename file over so no info in file name >>> magic.from_file('unknown') 'gzip compressed data, last modified: Mon Jan 28 22:49:19 2019, from Unix'
import subprocess import shlex path = "/path/to/myfile.tar.gz" cmd = shlex.split('file --mime-type {0}'.format(path)) result = subprocess.check_output(cmd) mime_type = result.split()[-1] print (mime_type)