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?
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().
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.
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 > '.
Probably others as well. "magic" is the magic keyword to search for. ;-)
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