473,756 Members | 4,256 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

exclude binary files from os.walk

rbt
Is there an easy way to exclude binary files (I'm working on Windows XP)
from the file list returned by os.walk()?

Also, when reading files and you're unsure as to whether or not they are
ascii or binary, I've always thought it safer to 'rb' on the read, is
this correct... and if so, what's the reasoning behind this? Again all
of this pertains to files on Windows XP and Python 2.4

Many thanks!
Jul 18 '05 #1
11 3227
On 2005-01-26, rbt <rb*@athop1.ath .vt.edu> wrote:
Is there an easy way to exclude binary files (I'm working on
Windows XP) from the file list returned by os.walk()?
Sure, assuming you can provide a rigorous definition of 'binary
files'. :)
Also, when reading files and you're unsure as to whether or
not they are ascii or binary, I've always thought it safer to
'rb' on the read, is this correct...
That depends on what you want. Adding a 'b' will disable the
cr/lf handling. Not sure what else it does.
and if so, what's the reasoning behind this?
Behind what?
Again all of this pertains to files on Windows XP and Python 2.4


--
Grant Edwards grante Yow! Now that I have my
at "APPLE", I comprehend COST
visi.com ACCOUNTING!!
Jul 18 '05 #2
rbt
Grant Edwards wrote:
On 2005-01-26, rbt <rb*@athop1.ath .vt.edu> wrote:

Is there an easy way to exclude binary files (I'm working on
Windows XP) from the file list returned by os.walk()?

Sure, assuming you can provide a rigorous definition of 'binary
files'. :)


non-ascii
Jul 18 '05 #3
There's no definitive way of telling a file is
"non-ascii". Bytes in a binary file define
perfectly good ascii characters. Windows
depends on file extensions to try to keep track
of the "type" of data in a file, but that isn't
foolproof. I can rename a plain ascii file with
a .EXE extension.

We could be of more help, if you would take the
time to explain a little about what you are trying
to do.

Larry Bates

rbt wrote:
Grant Edwards wrote:
On 2005-01-26, rbt <rb*@athop1.ath .vt.edu> wrote:

Is there an easy way to exclude binary files (I'm working on
Windows XP) from the file list returned by os.walk()?


Sure, assuming you can provide a rigorous definition of 'binary
files'. :)

non-ascii

Jul 18 '05 #4
you might want to look up the 'isascii' function...
i.e. - can be represented using just 7-bits.

Jul 18 '05 #5

"rbt" <rb*@athop1.ath .vt.edu> wrote in message
news:ct******** **@solaris.cc.v t.edu...
Is there an easy way to exclude binary files (I'm working on Windows XP)
from the file list returned by os.walk()?

Also, when reading files and you're unsure as to whether or not they are
ascii or binary, I've always thought it safer to 'rb' on the read, is this
correct... and if so, what's the reasoning behind this? Again all of this
pertains to files on Windows XP and Python 2.4


Please clarify: is your question about identifying binary (non-ascii) files
or about using os.walk?
Jul 18 '05 #6
On 2005-01-26, Larry Bates <lb****@syscono nline.com> wrote:
There's no definitive way of telling a file is "non-ascii".
Bytes in a binary file define perfectly good ascii characters.
As long as bit 7 is a 0.

Traditional ASCII only allows/defines the values 0x00 through
0x7f. If that's what is meant by "ASCII", then a file
containting bytes greater than 0x7F is not ASCII.

If all bytes are 0x7F or below, the file _may_ be ASCII, but
there's now way to tell if it _is_ ASCII unless you ask the
creator of the file. It could be Baudot or some other encoding
that doesn't use bit 7. Or, it could just be binary data that
happens to have bit 7 == 0.
We could be of more help, if you would take the time to
explain a little about what you are trying to do.


Yup.

--
Grant Edwards grante Yow! Now, let's SEND OUT
at for QUICHE!!
visi.com
Jul 18 '05 #7
On Wed, 2005-01-26 at 17:32 -0500, rbt wrote:
Grant Edwards wrote:
On 2005-01-26, rbt <rb*@athop1.ath .vt.edu> wrote:

Is there an easy way to exclude binary files (I'm working on
Windows XP) from the file list returned by os.walk()?

Sure, assuming you can provide a rigorous definition of 'binary
files'. :)


non-ascii


That's not really safe when dealing with utf-8 files though, and IIRC
with UCS2 or UCS4 as well. The Unicode BOM its self might (I'm not sure)
qualify as ASCII.

--
Craig Ringer

Jul 18 '05 #8
The OP wrote:
Is there an easy way to exclude binary files (I'm working on Windows

XP) from the file list returned by os.walk()?

Sure, piece of cake:

#!/usr/bin/env python

import os

def textfiles(path) :
include = ('.txt', '.csv',)
for root, dirs, files in os.walk(path):
for name in files:
prefix, ext = os.path.splitex t(name)
if ext.lower() not in include:
continue
filename = os.path.join(ro ot, name)
yield filename

path = os.getcwd()
for name in textfiles(path) :
print name

;-)

// m
Jul 18 '05 #9
rbt <rb*@athop1.ath .vt.edu> wrote:
Grant Edwards wrote:
On 2005-01-26, rbt <rb*@athop1.ath .vt.edu> wrote:
Is there an easy way to exclude binary files (I'm working on
Windows XP) from the file list returned by os.walk()?


Sure, assuming you can provide a rigorous definition of 'binary
files'. :)


non-ascii


The only way to tell for sure if a file contains only ASCII characters
is to read the whole file and check. You _are_, however, using a very
strange definition of "binary". A file of text in German, French or
Italian, for example, is likely to be one you'll define as "binary" --
just as soon as it contains a vowel with accent or diaeresis, for
example. On the other hand, you want to consider "non-binary" a file
chock full of hardly-ever-used control characters, just because the
American Standard Code for Information Interchange happened to
standardize them once upon a time? Most people's intuitive sense of
what "binary" means would rebel against both of these choices, I think;
calling a file "binary" because its contents are, say, the string
'El perro de aguas español.\n' (the n-with-tilde in "español"
disqualifies it from being ASCII), while another whose contents are 32
bytes all made up of 8 zero bits each (ASCII 'NUL' characters) is to be
considered "non-binary".

In any case, since you need to open and read all the files to check them
for "being binary", either by your definition or whatever heuristics you
might prefer, you would really not ``excluded them from os.walk'', but
rather filter os.walk's results by these criteria.
Alex
Jul 18 '05 #10

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

Similar topics

0
1815
by: Jesse Noller | last post by:
The problem: I am writing a file uploading utility in python that uses the walk() function to parse a directory, finding any file under that directory, and upload it to a remote server using the pyCURL curl interface. The files are invariably binary files, and the upload method is via an HTTP PUT to the system. I also need to perform the reverse - I need to GET those files and write them to disk. The problem I am seeing is memory....
1
1948
by: fishboy | last post by:
Howdy, I'm in middle of a personal project. Eventually it will download multipart binary attachments and look for missing parts on other servers. And so far I've got it to walk a newsgroup and download and decode single part binaries. I thought I'd post the code and see what people think. I'd appreciate any feedback. It's my first program with generators and I'm worried I'm making this twice and hard as it needs to be.
0
1868
by: fishboy | last post by:
Howdy, Sorry if this is a double post. First try seemed to go into hyperspace. I'm working on a personal project. It's going to be a multipart binary attachment downloader that will search alternate servers for missing pieces. This is the working code so far. It will walk a newsgroup and download and decode all the single part attachments. Just change server,user,password,group at the bottom to something less
5
4183
by: Anand K Rayudu | last post by:
Hi all, I am trying to find a way to get the files recursively in a given directory, The following code is failing, can some one please suggest what could be problem here from os import walk,join
7
3651
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
5
9572
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
7
3131
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following assumptions are, well, least unportable. In particular, do anyone know if there are real-life systems where the text file assumptions below don't hold? For text mode FILE*s,
4
1908
by: nguser3552 | last post by:
Hello everyone, I'm wondering if someone out there knows how in a visual c++ console application how I can do the following, and man I've tried, it seems simple really: I need to open up any file (the fnctl library offers hope), in binary or raw mode just 1' and 0's whichever is preferable. Once the file is open, attached to a stream and a buffer is created, I have to be able to take the final object as though
6
21114
by: D | last post by:
Hello, How can one exclude a directory (and all its subdirectories) when running os.walk()? Thanks, Doug
0
9462
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...
1
9857
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
8723
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
6542
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
5155
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...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
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
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2677
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.