473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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 3195
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.vt.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****@syscononline.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.splitext(name)
if ext.lower() not in include:
continue
filename = os.path.join(root, 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
Craig Ringer <cr***@postnewspapers.com.au> wrote:
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.


Nope, both bytes in the BOM have the high-order bit set -- they're 0xFF
and 0xFE -- so they definitely don't qualify as ASCII.
Alex
Jul 18 '05 #11
On Wed, 26 Jan 2005 18:25:09 -0500, "Dan Perl" <da*****@rogers.com> wrote:

"rbt" <rb*@athop1.ath.vt.edu> wrote in message
news:ct**********@solaris.cc.vt.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?

I have a feeling it's about walking directories and identifying which files
to should be "cooked" (to normalize line endings when opened and read).

Regards,
Bengt Richter
Jul 18 '05 #12

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

Similar topics

0
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...
1
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...
0
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...
5
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...
7
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
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
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...
4
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...
6
by: D | last post by:
Hello, How can one exclude a directory (and all its subdirectories) when running os.walk()? Thanks, Doug
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.