determine file type

M

Mark Gibson

Is there an equivalent to the unix 'file' command?

[mark tmp]$ file min.txt
min.txt: ASCII text
[mark tmp]$ file trunk
trunk: directory
[mark tmp]$ file compliance.tgz
compliance.tgz: gzip compressed data, from Unix

What I really want to do is determine if a file is 1) a directory, 2) a
text file 3) a binary file.

Is there a way to do this?

Mark
 
J

James Stroud

Mark said:
Is there an equivalent to the unix 'file' command?

[mark tmp]$ file min.txt
min.txt: ASCII text
[mark tmp]$ file trunk
trunk: directory
[mark tmp]$ file compliance.tgz
compliance.tgz: gzip compressed data, from Unix

What I really want to do is determine if a file is 1) a directory, 2) a
text file 3) a binary file.

Is there a way to do this?

Mark

import os
def test_file(filename, maxread=1024):
if os.path.isdir(filename):
return 'directory'
afile = open(filename) # open as text
for achar in afile.read(maxread):
if ord(achar) > 127:
return 'binary'
return 'text'


James



--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
M

Mark Gibson

import os
def test_file(filename, maxread=1024):
if os.path.isdir(filename):
return 'directory'
afile = open(filename) # open as text
for achar in afile.read(maxread):
if ord(achar) > 127:
return 'binary'
return 'text'

Pefect, thanks!
 
S

Steven D'Aprano

Pefect, thanks!

Not only is it not perfect, as given it isn't even correct.
'text'

However, with a more careful algorithm for deciding what's text and what's
not, the general approach is fine.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top