Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file contains plain text?

Tags:

python

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.

like image 206
daniels Avatar asked Mar 18 '10 17:03

daniels


People also ask

How can you tell if a file is plain text?

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.

Which file is in plain text form?

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.

What does a plain text file contain?

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.


1 Answers

Use Python's mimetypes library:

import mimetypes
if mimetypes.guess_type('full path to document here')[0] == 'text/plain':
    # file is plaintext
like image 167
Mike Cialowicz Avatar answered Sep 29 '22 22:09

Mike Cialowicz