473,761 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

determine file type

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
Mar 27 '06 #1
3 2434
Mark Gibson wrote:
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(filen ame, maxread=1024):
if os.path.isdir(f ilename):
return 'directory'
afile = open(filename) # open as text
for achar in afile.read(maxr ead):
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/
Mar 27 '06 #2


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


Pefect, thanks!

Mar 27 '06 #3
On Sun, 26 Mar 2006 19:52:32 -0700, Mark Gibson wrote:


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


Pefect, thanks!


Not only is it not perfect, as given it isn't even correct.
open("lots_of_n ulls.bin", "w").write( "\0" * 1024)
test_file("lots _of_nulls.bin")

'text'

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

Mar 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2183
by: bart plessers | last post by:
Hello, I have a form with some checkboxes, i.e. The default value of this checkbox is determined in a global file (config.asp), that is included in first line of the form, i.e. ShowFilenames = 1 The first time the form is displayed, the value of ShowFilenames should be
18
2888
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code that applies to all these events, but I need to have specific code execute when the form closes. The properties for this method are sender (the originator) and e (event arguments). I know how to get typeof (sender) to determine what form or...
12
7837
by: Sunner Sun | last post by:
Hi, all Since the OS look both ASCII and binary file as a sequence of bytes, is there any way to determine the file type except to judge the extension? Thank you!
6
14339
by: Kaki | last post by:
Given a file, how do I know if it's ascii or unicode or binary? And how do I know if it's rtf or html or etc? In other words, how do I find the stream type or mime type? (No, file extension cannot be the answer) Thanks *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
3
1738
by: Lance | last post by:
Is there any way to determine the type of an object that is stored as a serialized file without deserializing the entire file? If possible, I'm interested in being able to do this for both binary and XML files. Thanks, Lance
9
24208
by: Thomas Podlesak | last post by:
I need a check, if a file is utf8 encoded. I only found the php-functions 'iconv' and 'recode'. But it seems it´s not possible to determine the encoding with them. Isn´t there any similar function to the 'file'-command on linux for php?
28
20662
by: Tim Daneliuk | last post by:
I have a program wherein I want one behavior when a file is set as executable and a different behavior if it is not. Is there a simple way to determine whether a given named file is executable that does not resort to all the lowlevel ugliness of os.stat() AND that is portable across Win32 and *nix? Thanks, ---------------------------------------------------------------------------- Tim Daneliuk tundra@tundraware.com PGP Key: ...
1
2562
by: topramen | last post by:
does any one here know of a good way to to determine whether or not a given path is a directory (e.g., "c:\mydir") or a file (e.g., "c:\mydir \myfile.txt")? i started attacking this problem by using filesystemobject to check whether or not a directory existed for the path. if so, it was a directory path. if not, i checked to see if a file existed for it. if so, it was a file path. this code is shown below: ...
4
2979
by: =?Utf-8?B?R3JlZw==?= | last post by:
I am a newbie to WCF so please forgive if this is an obvious question. I have the following. (Service contract) IEmployee public interface IEmployee { List<EmployeeGetEmployeeByID(string employeeID);
1
4823
by: Thi | last post by:
Hi, I am trying to develop an application that allows the users to drag a few file(s) from a zip archive to a destination. My question is, how do i determine where the drop destination is because i need to do some validation checking before extracting the files from the zip archive (e.g. validate archive password). Any help or some sample codes would be very much appreciated.
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.