I needed a way to determine a file's type within a
Python
script when I can't rely on the file's extension to determine the
file format. I'll be
running the script on a MacBook Pro laptop running the
OS X El Capitan
operating system.
OS X/macOS, like
Linux, comes with the
file command, so
I could run that command at a
shell prompt to
have the utility check the
magic number in the files I'm interested in, but I want to do some
additional processing of the files within the Python script, so I want to
perform the format check within Python. Python provides the
subprocess
module that provides the capability to "spawn new processes, connect
to their input/output/error pipes, and obtain their return codes." So I can
call the file utility from
within Python using that module. To get the results from running a shell
command, you use suprocess.Popen()
. You can then set a
variable
to hold the results of
.communicate() and print the contents of that variable as shown below.
The script expects the name of the file to be checked to be provided as
an
argument on the command line.
#!/usr/bin/python import subprocess as sub, sys try: sys.argv[1] except IndexError: print "Error - missing input file name! Usage ./filetype.py infile" sys.exit(1) else: fileName = sys.argv[1] p = sub.Popen(['file',fileName],stdout=sub.PIPE,stderr=sub.PIPE) output, errors = p.communicate() print output
[ More Info ]