[Tutor] File extension against File content

Alan Gauld alan.gauld at yahoo.co.uk
Fri May 31 09:58:12 EDT 2019


On 31/05/2019 11:03, Sunil Tech wrote:
> Hi Tutor,
> 
> Is there any way that I can actually programmatically compare the file
> extension with its content?

For images the standard library offers imghdr I'm not sure how
reliable or accurate it is but it claims to identify a dozen
or so  of the most common formats.

For sound there is the similar sndhdr module which tries to do
the same thing for audio files.

There are also modules for reading exif data from image files and ID
tags from audio files. The existence of such tags can indicate that the
file is of the appropriate type. Combine with try/except to test a file...

> This is because we can manually save the file in one extension and later
> rename the file extension to some other.

And indeed store the file with no extension at all.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list