Python version of perl's "if (-T ..)" and "if (-B ...)"?

L

Lloyd Zusman

Perl has the following constructs to check whether a file is considered
to contain "text" or "binary" data:

if (-T $filename) { print "file contains 'text' characters\n"; }
if (-B $filename) { print "file contains 'binary' characters\n"; }

Is there already a Python analog to these? I'm happy to write them on
my own if no such constructs currently exist, but before I start, I'd
like to make sure that I'm not "re-inventing the wheel".

By the way, here's what the perl docs say about these constructs. I'm
looking for something similar in Python:

.... The -T and -B switches work as follows. The first block or so
.... of the file is examined for odd characters such as strange control
.... codes or characters with the high bit set. If too many strange
.... characters (>30%) are found, it's a -B file; otherwise it's a -T
.... file. Also, any file containing null in the first block is
.... considered a binary file. [ ... ]

Thanks in advance for any suggestions.
 
A

Aahz

Perl has the following constructs to check whether a file is considered
to contain "text" or "binary" data:

if (-T $filename) { print "file contains 'text' characters\n"; }
if (-B $filename) { print "file contains 'binary' characters\n"; }

Assuming you're on a Unix-like system or can install Cygwin, the
standard response is to use the "file" command. It's *much* more
sophisticated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top