I have a folder full of files and I want to search some string inside them. The issue is that some files may be zip, exe, ogg, etc. Can I check somehow what kind of file is it so I only open and search through txt, PHP, etc. files. I can't rely on the file extension.
For some operating systems like Windows, we can easily determine the type of a file by looking at the extension in the filename. For example, files with the “. txt” extension are plain ASCII text files.
Plain text (. txt) is a type of digital file that is free of computer tags, special formatting, and code. This is the only file type recognized by the Lexile Analyzer. Note: Copying and pasting or uploading text and text files other than plain text may include computer tags, special formatting, and code.
A plain text file is a document that contains no formatting, images, colors or other types of markup. It also includes single line breaks and spacing.
Use Python's mimetypes
library:
import mimetypes
if mimetypes.guess_type('full path to document here')[0] == 'text/plain':
# file is plaintext
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With