Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a python-equivalent of the unix "file" utility?

Tags:

python

unix

I want to have different behavior in a python script, depending on the type of file. I cannot use the filename extension as it may not be present or misleading. I could call the file utility and parse the output, but I would rather use a python builtin for portability.

So is there anything in python that uses heuristics to deduce the type of the file from its contents?

like image 765
0x89 Avatar asked Dec 29 '09 13:12

0x89


People also ask

How do I read a Unix file in Python?

Example 1: Reading file using read(), readline() and readlines() Create a file named read1.py with the following python script. It will read the file based on the byte size using read(), read the fixed number of characters from a file using readline() and read all lines of a file in an array using readlines().

Can PYTHON be used in Linux?

Python comes preinstalled on most Linux distributions, and is available as a package on all others. However there are certain features you might want to use that are not available on your distro's package. You can easily compile the latest version of Python from source.

What is touch in Python?

In Python, the Touch module is used to create any file on any specified directory. The touch module is equivalent to linux command ' touch ' or to the windows' cmd command ' type null > '.


1 Answers

  • python-magic
  • pymagic

Probably others as well. "magic" is the magic keyword to search for. ;-)

like image 50
Alex Brasetvik Avatar answered Sep 30 '22 16:09

Alex Brasetvik