473,569 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Testing for file type

Hi Everyone,

Is there a way to test if a file is binary or ascii within Python?

I'd prefer not to text against file extension.

Any help you can provide would be greatly appreciated.

Thanks,
Andy
May 22 '06 #1
3 1236
Andrew Robert wrote:
Hi Everyone,

Is there a way to test if a file is binary or ascii within Python?

try:
open(filname).r ead().decode("a scii")
ascii = True
except UnicodeError:
ascii = False

Diez
May 22 '06 #2
Andrew Robert a écrit :
Hi Everyone,

Is there a way to test if a file is binary or ascii within Python?

I'd prefer not to text against file extension.

Any help you can provide would be greatly appreciated.


May look at unix "file" command sources...
A+

L.Pointal.
May 22 '06 #3
In the Python Cookbook, 2ed, page 25, there's a script by Andrew Dalke
that uses the same heureistic criteria that Perl does and analyzes a
string and deems it binary if it contains any nulls or if more than 30%
of its characters have the high bit set or are strange control codes.

There's a follow on script that uses the same test on files.

rick

May 22 '06 #4

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

Similar topics

2
2081
by: Edvard Majakari | last post by:
Hi all ya unit-testing experts there :) Code I'm working on has to parse large and complex files and detect equally complex and large amount of errors before the contents of the file is fed to module interpreting it. First I created a unit-test class named TestLoad which loaded, say, 40 files of which about 10 are correct and other 30 files...
0
1745
by: Jonathan Allen | last post by:
We have found that our method of testing does not match traditional text-book methodologies. I decided to write a short white paper on it so that I could get your opinions. Does anyone else use this method? If so, what the heck is it called? Do you think it would work in other projects, or did we just luck out? Jonathan Allen ...
1
1804
by: morphex | last post by:
Hi, I get the following messages running the testall.py script with m2crypto 0.13, can anyone tell me what's wrong? .................................................................EEEEEE ====================================================================== ERROR: test_cipher_mismatch (test_ssl.SSLClientTestCase)...
3
2434
by: Joe Van Dyk | last post by:
Say I've written a class that wraps around a particular (complex) communication service library. I want to unit test objects that use that communication service. Is the "best practice" to create a Communication_Service abstract base class that my Concrete_Communication_Service and my Mock_Communication_Service both inherit from? (and...
44
2821
by: John A. Bailo | last post by:
Dr. Dobbs has a /glowing/ article on Ruby on Rails this month. What do you guys think? Can it replace .net, php and java? And be the Open Source OOP web solution that is not bound to Sun or MS? http://media.rubyonrails.org/presentations/pursuitofbeauty.pdf
18
2369
by: Andrew Wan | last post by:
I have been developing web applications with ASP & Javascript for a long time. I have been using Visual Studio 2003.NET. While VS2003 is okay for intellisense of ASP & Javascript, it's still not that great. One of the cons of ASP & Javascript is that they're both interpreted, which means one has twice the amount of work to do interms of...
3
5048
by: dOleAviSc | last post by:
i have an input file with several numbers (ie 73296^4832 /n) carrot being a space and /n meaning theres another line of similar type numbers underneath. i'm trying to read them in one at a time into an array as char type and then converting them to integers, easy enough. the problem i'm having is i need my while loop to stop at the end of each...
0
1361
by: Matthew Fitzgibbons | last post by:
I'm by no means a testing expert, but I'll take a crack at it. Casey McGinty wrote: I've never run into this. Rule of thumb: always separate software from hardware. Write mock classes or functions that do your hardware/file access that always return known data (but remember to test for alpha and beta errors--make sure both valid and...
4
1973
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I am familiar with unit testing but have to implement in a large solution of 100 projects - my concern is Adding Unit test will bloat the solution - taking more time to get all code from SVN and slow compile time for developers -Is there a way to build solution in debug without building all the unit tests -Is there a goos way to...
3
2195
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus that are SELECT elements (javascript-- one named arrivalcity and one named returncity). Basically they ask for a departure and return city and another...
0
7697
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...
1
7672
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...
0
6283
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...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
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...

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.