Re: [Tutor] File extension against File content
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] File extension against File content
On 31May2019 12:38, ingo wrote: Many file formats have "magic bytes" that you can use for that purpose. https://en.wikipedia.org/wiki/List_of_file_signatures Also, UNIX systems ship with a command called "file" which inspects a file's leading data for such magic numbers to identify their content. And there's a library called libmagic [1,2] which does this work, and there's a PyPI package called python-magic [3] for using this from Python, not to mention various other PyPI modules [4]. 1: https://sourceforge.net/projects/libmagic/ 2: https://github.com/threatstack/libmagic 3: https://pypi.org/project/python-magic/ 4: https://pypi.org/search/?q=magic Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] File extension against File content
Many file formats have "magic bytes" that you can use for that purpose. https://en.wikipedia.org/wiki/List_of_file_signatures Ingo On 31-5-2019 12:03, Sunil Tech wrote: > Hi Tutor, > > Is there any way that I can actually programmatically compare the file > extension with its content? > > This is because we can manually save the file in one extension and later > rename the file extension to some other. > > Thanks > - Sunil. G > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] File extension against File content
Hi Tutor, Is there any way that I can actually programmatically compare the file extension with its content? This is because we can manually save the file in one extension and later rename the file extension to some other. Thanks - Sunil. G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor