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
Follow my photo-blog on Flickr at:

Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:

Reply via email to