[perl-python] 20050127 traverse a dir

X

Xah Lee

# -*- coding: utf-8 -*-
# Python

suppose you want to walk into a directory, say, to apply a string
replacement to all html files. The os.path.walk() rises for the
occasion.

© import os
© mydir= '/Users/t/Documents/unix_cilre/python'
© def myfun(s1, s2, s3):
© print s2 # current dir
© print s3 # list of files there
© print '------==(^_^)==------'
© os.path.walk(mydir, myfun, 'somenull')


----------------------
os.path.walk(base_dir,f,arg) will walk a dir tree starting at
base_dir, and whenever it sees a directory (including base_dir), it
will call f(arg,current_dir,children), where the current_dir is the
string of the current directory, and children is a *list* of all
children of the current directory. That is, a list of strings that are
file names and directory names. Try the above and you'll see.

now, suppose for each file ending in .html we want to apply function
g to it. So, when ever myfun is called, we need to loop thru the
children list, find files and ending in html (and not a directory),
then call g. Here's the code.

© import os
© mydir= '/Users/t/web/SpecialPlaneCurves_dir'
© def g(s): print "g touched:", s
© def myfun(dummy, dirr, filess):
© for child in filess:
© if '.html' == os.path.splitext(child)[1] \
© and os.path.isfile(dirr+'/'+child):
© g(dirr+child)
© os.path.walk(mydir, myfun, 3)

note that os.path.splitext splits a string into two parts, a portion
before the last period, and the rest in the second portion. Effectively

it is used for getting file suffix. And the os.path.isfile() make sure
that this is a file not a dir with .html suffix... Test it yourself.

one important thing to note: in the mydir, it must not end in a
slash. One'd think Python'd take care of such trivia but no. This took
me a while to debug.

also, the way of the semantics of os.path.walk is nice. The myfun can
be a recursive function, calling itself, crystalizing a program's
semantic.

---------------------------
# in Perl, similar program can be had.
# the prototypical way to traverse a dir
# is thru File::Find;

use File::Find qw(find);
$mydir= '/Users/t/web/SpecialPlaneCurves_dir';
find(\&wanted, $mydir);
sub g($){print shift, "\n";}
sub wanted {
if ($_ =~/\.html$/ && -T $File::Find::name) { g $File::Find::name;}
$File::Find::name;
}

# the above showcases a quick hack.
# File::Find is one of the worst module
# there is in Perl. One cannot use it
# with a recursive (so-called) "filter"
# function. And because the way it is
# written, one cannot make the filter
# function purely functional. (it relies
# on the $_) And the filter function
# must come in certain order. (for
# example, the above program won't work
# if g is moved to the bottom.) ...

# the quality of modules in Perl are
# all like that.
Xah
(e-mail address removed)
http://xahlee.org/PageTwo_dir/more.html
 
C

Chris Mattern

Xah Lee wrote:

# the above showcases a quick hack.
# File::Find is one of the worst module
# there is in Perl. One cannot use it
# with a recursive (so-called) "filter"
# function. And because the way it is
# written, one cannot make the filter
# function purely functional. (it relies
# on the $_) And the filter function
# must come in certain order. (for
# example, the above program won't work
# if g is moved to the bottom.) ...

# the quality of modules in Perl are
# all like that.

Is it just me, or is the disappointing lack of flamewars
slowly ratcheting up the level of vitriol in his posts?

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
T

The Flow

Xah Lee,

Do you want to be taken seriously?
First, stop posting.
Second, learn perl.
Third, learn python.
 
T

Timo Virkkala

The said:
Do you want to be taken seriously?
First, stop posting.
Second, learn perl.
Third, learn python.

No. Second, learn Python. Third, learn Perl (optional). :)
But we do agree on the first.
 
T

The Flow

Sorry about that... (I forgot what he was trying to teach)
Thanks for the clarification
 
J

Jürgen Exner

Xah Lee wrote:
[...]
[long rant about Perl modules snipped]

# And because the way it is
# written,

Yeah, indeed, you correctly identified the root of the problems.
If you would have written your Perl program in a normal way instead of in
your cryptic wretched style then you would not have had any of the issues
that you listed.
Even the best programming language in the world cannot stop a bad programmer
from writing poor code.

But like my old professor always used to say: No program is useless. It can
always be used as an example for how not to do things.

jue
 
T

Terry Reedy

The Flow said:
Xah Lee,

Do you want to be taken seriously?
First, stop posting.
Second, learn perl.
Third, learn python.

Hey all, I have seen no evidence that XL even reads the responses that have
been directed thereto. The above is like

/dev/null,
Why don't you ever answer the messages I keep sending to you?

Terry J. Reedy
 
D

Dan Perl

Terry Reedy said:
Hey all, I have seen no evidence that XL even reads the responses that
have been directed thereto. The above is like

/dev/null,
Why don't you ever answer the messages I keep sending to you?

Terry J. Reedy

According to his own website, he suspects himself of having a schizoid
personality. This is how Britannica Online describes "schizoid":
"In this disorder there is a disinclination to mix with others, the
individual appearing aloof, withdrawn, indifferent, unresponsive, and
disinterested. Such a person prefers solitary to gregarious pursuits,
involvement with things rather than with people, and often appears
humourless or dull."

Does that explain things for you? I really think that nothing we say gets
through to him.

Dan
 
A

Abigail

Timo Virkkala ([email protected]) wrote on MMMMCLXVIII September MCMXCIII in
<URL:mad:@ The Flow wrote:
@@ > Do you want to be taken seriously?
@@ > First, stop posting.
@@ > Second, learn perl.
@@ > Third, learn python.
@@
@@ No. Second, learn Python. Third, learn Perl (optional). :)


Just leave the third option out. Let him learn Python. We don't want him. ;-)


Abigail
 
T

Timo Virkkala

Xah said:
Thanks to those who have made useful comments. They will be
assimilated.

The comments, or those who have made comments?

"Corrections are futile. You will be assimilated."
--Xah Lee
 
D

David H Wild

Hey all, I have seen no evidence that XL even reads the responses that
have been directed thereto. The above is like
/dev/null,
Why don't you ever answer the messages I keep sending to you?

He's been in my killfile for quite some time. If **everyone** ignored him,
this newsgroup would be a better place. :))
 
S

Skip Montanaro

abigail> @@ No. Second, learn Python. Third, learn Perl (optional). :)

abigail> Just leave the third option out. Let him learn Python. We don't
abigail> want him. ;-)

We don't want him either. Perhaps we can persuade him to learn INTERCAL...

Skip
 
A

Abigail

Skip Montanaro ([email protected]) wrote on MMMMCLXVIII September MCMXCIII
in <URL:__
__ abigail> @@ No. Second, learn Python. Third, learn Perl (optional). :)
__
__ abigail> Just leave the third option out. Let him learn Python. We don't
__ abigail> want him. ;-)
__
__ We don't want him either. Perhaps we can persuade him to learn INTERCAL...


Please don't send stealth CCs.


Abigail
 
J

Jeremy Bowers

Is it just me, or is the disappointing lack of flamewars
slowly ratcheting up the level of vitriol in his posts?

What flabbergasts me is the stunning failure in trolling that XL is.

I've accidentally trolled (if you can extend the trolling definition that
way) through ignorance of both subject matter and local culture,
accidentally trolled through typo, and accidentally trolled through poorly
chosen incendiary example that had little to do with my point.

This poor guy trolls across five newsgroups and is now one of the few
things that they absolutely all absolutely agree on.

Now *that* is some truly breathtaking failure right there. I'm not sure I
could fail that hard if I tried.

(I'll shut up about Xah now.)
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top