Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [perl-python] 20050127 traverse a dir

Reply
Thread Tools

[perl-python] 20050127 traverse a dir

 
 
Xah Lee
Guest
Posts: n/a
 
      01-27-2005
# -*- 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://xahlee.org/PageTwo_dir/more.html

 
Reply With Quote
 
 
 
 
Chris Mattern
Guest
Posts: n/a
 
      01-27-2005
Xah Lee wrote:

<snip>
>
> # 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?"
 
Reply With Quote
 
 
 
 
The Flow
Guest
Posts: n/a
 
      01-28-2005
Xah Lee,

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

 
Reply With Quote
 
Timo Virkkala
Guest
Posts: n/a
 
      01-28-2005
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).
But we do agree on the first.

--
Timo Virkkala
 
Reply With Quote
 
The Flow
Guest
Posts: n/a
 
      01-28-2005
Sorry about that... (I forgot what he was trying to teach)
Thanks for the clarification

--
The Flow

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      01-28-2005
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


 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      01-28-2005

"The Flow" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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



 
Reply With Quote
 
Dan Perl
Guest
Posts: n/a
 
      01-28-2005

"Terry Reedy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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


 
Reply With Quote
 
Abigail
Guest
Posts: n/a
 
      01-28-2005
Timo Virkkala ((E-Mail Removed)) wrote on MMMMCLXVIII September MCMXCIII in
<URL:news:nGgKd.620$(E-Mail Removed)>:
@@ 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
--
sub f{sprintf'%c%s',$_[0],$_[1]}print f(74,f(117,f(115,f(116,f(32,f(97,
f(110,f(111,f(116,f(104,f(0x65,f(114,f(32,f(80,f(1 01,f(114,f(0x6c,f(32,
f(0x48,f(97,f(99,f(107,f(101,f(114,f(10,q ff)))))))))))))))))))))))))
 
Reply With Quote
 
Xah Lee
Guest
Posts: n/a
 
      01-28-2005
for those interested, i've created a webpage for these perl-python
daily tips. The url is:
http://xahlee.org/perl-python/python.html

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

Xah
(E-Mail Removed)
http://xahlee.org/PageTwo_dir/more.html



> Hey all, I have seen no evidence that XL even reads the responses

that have
> been directed thereto.
> Why don't you ever answer the messages
> ...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dir() vs print(dir()) in the embedded mode Nick Gnedin Python 2 03-30-2013 05:52 AM
External prog get file from main dir, want file in diff dir Kga Agk Ruby 2 06-29-2009 02:09 PM
Behaviour of Dir.glob("**/b") and Dir.glob("*/b") for symlinks Matthew Denner Ruby 1 08-04-2006 03:42 PM
Sub Dir, Virtual dir, what do I use? =?Utf-8?B?UnVkeQ==?= ASP .Net 0 06-12-2005 08:25 PM
[perl-python] 20050127 traverse a dir Xah Lee Perl Misc 10 01-29-2005 07:26 PM



Advertisments