Python Forum
getting file type information
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting file type information
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
(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?

I would like to know how to get the type of a file
Reply
#3
There are some 3-party libaries that do this eg python-magic, filetype, fleep.
python-magic has best support of file-types as it eg can detected .py which the other i mention can not.
Test:
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'
Reply
#4
i only need file system semantic types, e.g. regular file, directory, symbolic link, character device, block device, named socket, pipe. but this looks interesting. it can't understand that uncompressed it is a tar file? i wonder what other compression formats it understands.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
to get the mime type in Linux you can do

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)
Reply
#6
to what extent can shlex provide the separate values for HTTP headers Content-Type: and Content-Encoding:? it would need to recognize the major compression formats and actually do enough decompression to recognize what's inside.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  detecting type of compression of a file Skaperen 1 2,774 Oct-30-2020, 11:04 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020