Filetype information

I'm in the process of writing a python script, and I want to find out information about a file, such as for example a mime-type (or any useful depiction of what a file contains).

I've heard about python-magic, but I'm really looking for the solution that will allow me to find this information, without requiring the installation of additional packages.

Am I stuck to maintaining a list of file extensions, or does python have something in the standard library? I was not able to find it in the docs.


Asked by: Lucas217 | Posted: 06-12-2021






Answer 1

I am not sure if you want to infer something from file content but if you want to know mime type from file extension mimetypes module will be sufficient

>>> import mimetypes
>>> mimetypes.init()
>>> mimetypes.knownfiles
['/etc/mime.types', '/etc/httpd/mime.types', ... ]
>>> mimetypes.suffix_map['.tgz']
'.tar.gz'
>>> mimetypes.encodings_map['.gz']
'gzip'
>>> mimetypes.types_map['.tgz']
'application/x-tar-gz'

http://docs.python.org/library/mimetypes.html

Answered by: Emily540 | Posted: 07-01-2022



Answer 2

The standard library has support for mapping filenames to mimetypes.

Your question also sounds like you are interested in other information besides mimetype. The stat module will also give you information about size, owner, time of last modification, etc. but otherwise the most common filesystems (Windows NTFS/FAT, Linux Ext 2/3, Mac OS X) do not store any metadata or "other information" about files. That's why we need to use extensions to find the mimetype for example.

Answered by: Emily995 | Posted: 07-01-2022



Similar questions

How to know the filetype through python

In the very beginning,I tried to do like this(hoped to get some useful information in the head): >>content=open("fileurl","rb").read() I found that PNG (png)'s Header is lik this: 89504E47 (I don't know wheather it's true or not) But when I did in this way,the result is: >>> content[:20] '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01\x90' the \xis wha...


file - Python frozen app filetype

I've got a python GUI application (wx) that is able to open zip files with an alternate extension (.giz). def GetGIZData(self, filepath): #{extract the zip to a temp dir and read the files} pass My question is this: if I've got a file at C:\InputFile.giz, how do I get a handle on "C:\InputFile.giz" from a frozen python app in Windows when I double click on InputFile.giz? (Essentia...


python - batch file to open custom filetype

I have a custom filetype (.photon) that is read by a python script which I generally run like this in a terminal: py C:\Users\greym\Desktop\photon\photon.py C:\Users\greym\Desktop\photon\test.photon the last argument is the file that is read by the python script how I make it so I can click on that file and it will run it through the python script


Trying to open a certain filetype using my python exe

I made a simple python file encryptor. I want to open all encrypted (or .aes) files using my program. I have already made it into a working executable using pyinstaller but when I tried to open a .aes file with my program (through the context menu in the file explorer), it unsurprisingly started the program but did not take the .aes file as input. I want to open any .aes file using my program and for the python script to s...


python - Get filetype from BASE64 file

What is the way to get file type from base64 string? I need a solution which will work on Windows and Linux(Centos7) E.G.: This is string ; eHh4eHh4 which is = to a text file with xxxxxx inside. If websites can do this, I guess it is possible to do it in python: https://base64.guru/conve...


How to know the filetype through python

In the very beginning,I tried to do like this(hoped to get some useful information in the head): >>content=open("fileurl","rb").read() I found that PNG (png)'s Header is lik this: 89504E47 (I don't know wheather it's true or not) But when I did in this way,the result is: >>> content[:20] '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01\x90' the \xis wha...


VIM python filetype specific indent fails after 50 lines of one list

I'm currently working on reformatting a python project to follow the 4 spaces indent style. The project is being done in VIM with the following plugins: fugitive, snipmate, surround, git, supertab, minibufexpl, command-t. pyflakes-pathogen, ack, gundo, pydoc, pep8, py.test, makegreen, tasklist, nerdtree, ropevim . My .vimrc is currently: set tabstop=4 set shiftwidth=4 set expandtab let mapleader="...


python - Django- Check the filetype of an uploaded file and then alter the template

I want to write a function that checks the extension of the uploaded file and depending on that assigns a variable a value. I hope it makes sense. I basically want to check the filetype of an uploaded file and then alter the template accordingly. The code that I am thinking of implementing is something like this but I am kind of confused models.py class ScribbleMedia(models.Model): ...


How to serve filetype object in python

I'm using the URLLib2 method to download a file from another server via a rest api (the url can't be exposed to the user--that's why it needs to be done on the backend). It gives me the following response: (<addinfourl at 4365818480 whose fp = <google.appengine.dist27.socket._fileobject object at 0x1043883d0>> I'm now trying to find a way to serve this file to the end...


Python: A way to detect a filetype attached to a string?

In IronPython 2.6*, I'm trying to build a function that "corrects" a string; I have two arguments, FILE and EXTN. The idea is for them to be concatenated as necessary later in the program, but you know some people don't read instructions and you're bound to have someone enter "FILE.*" as their FILE, which would mess everything up. I'm looking for a way to take


file - Python frozen app filetype

I've got a python GUI application (wx) that is able to open zip files with an alternate extension (.giz). def GetGIZData(self, filepath): #{extract the zip to a temp dir and read the files} pass My question is this: if I've got a file at C:\InputFile.giz, how do I get a handle on "C:\InputFile.giz" from a frozen python app in Windows when I double click on InputFile.giz? (Essentia...


Python: How to find google drive filetype?

I'm currently using the session.get() code found at this stackoverflow. When I save the drive files they don't have a filetype suffix, so I have to add one manually to based on file type to open it. Is there a way I can parse the chunk and get filetype variable or maybe even search by hexcode? Better method? import requests def download_file_from_google_drive(id, destination): URL = "https://docs.google...


python - batch file to open custom filetype

I have a custom filetype (.photon) that is read by a python script which I generally run like this in a terminal: py C:\Users\greym\Desktop\photon\photon.py C:\Users\greym\Desktop\photon\test.photon the last argument is the file that is read by the python script how I make it so I can click on that file and it will run it through the python script


python - Save As, each filetype has different callback function

I use python tkinter, and Import tkinter.filedialog to save as files. The program has functions to save each file type. Using filedialog.asksaveasfilename Returns file path only. The Problem: how to get the file type, to call the right function? Below is a sample Code of what I did. python filename= filedialog.asksav...


Trying to open a certain filetype using my python exe

I made a simple python file encryptor. I want to open all encrypted (or .aes) files using my program. I have already made it into a working executable using pyinstaller but when I tried to open a .aes file with my program (through the context menu in the file explorer), it unsurprisingly started the program but did not take the .aes file as input. I want to open any .aes file using my program and for the python script to s...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top