473,498 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[perl-python] 20050127 traverse a dir

# -*- 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
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #1
15 2067
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?"
Jul 18 '05 #2
Xah Lee,

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

Jul 18 '05 #3
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
Jul 18 '05 #4
Sorry about that... (I forgot what he was trying to teach)
Thanks for the clarification

--
The Flow

Jul 18 '05 #5
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
Jul 18 '05 #6

"The Flow" <an**************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr 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

Jul 18 '05 #7

"Terry Reedy" <tj*****@udel.edu> wrote in message
news:ma***************************************@pyt hon.org...

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
Jul 18 '05 #8
Timo Virkkala (a@a.invalid) wrote on MMMMCLXVIII September MCMXCIII in
<URL:news:nG***************@read3.inet.fi>:
@@ 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)))))))))))))))))))))))))
Jul 18 '05 #9
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
xa*@xahlee.org
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
...


Jul 18 '05 #10
Xah Lee wrote:
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

--
Timo Virkkala
Jul 18 '05 #11
Xah Lee wrote:
daily tips. The url is:
http://xahlee.org/perl-python/python.html

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

Resistance is futile.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
Jul 18 '05 #12

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
Jul 18 '05 #13
Skip Montanaro (sk**@pobox.com) wrote on MMMMCLXVIII September MCMXCIII
in <URL:news:ma************************************** *@python.org>:
__
__ 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
--
perl -wle\$_=\<\<EOT\;y/\\n/\ /\;print\; -eJust -eanother -ePerl -eHacker -eEOT
Jul 18 '05 #14
In article <ma***************************************@python. org>,
Terry Reedy <tj*****@udel.edu> wrote:
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?


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

--
__ __ __ __ __ ___ _____________________________________________
|__||__)/ __/ \|\ ||_ | / Acorn StrongArm Risc_PC
| || \\__/\__/| \||__ | /...Internet access for all Acorn RISC machines
___________________________/ dh****@argonet.co.uk
Jul 18 '05 #15
On Thu, 27 Jan 2005 15:01:12 -0500, Chris Mattern wrote:
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.)

Jul 18 '05 #16

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

Similar topics

4
8118
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
3
3439
by: David F. Skoll | last post by:
Hi, I'm tearing my hair out on this one. I'm trying to embed a Perl interpreter into a C program. I need to be able to create and destroy the interpreter periodically, but will never actually...
0
9733
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
13
3221
by: Otto J. Makela | last post by:
I'm trying to install to php the Perl-1.0.0.tgz package (from http://pecl.php.net/package/perl, enabling one to call perl libraries) to a pre-existing Solaris system. Unfortunately, the attempt...
6
2979
by: surfivor | last post by:
I may be involved in a data migration project involving databases and creating XML feeds. Our site is PHP based, so I imagine the team might suggest PHP, but I had a look at the PHP documentation...
4
3668
by: billb | last post by:
I installed a perl extension for PHP to use some perl inside my php primarily because I have perl working with oracle and not php and oracle. So I want to use my old perl scripts, and use the...
21
34316
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
1
47344
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
7126
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
7005
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
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7381
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
4595
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...
0
3096
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...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
293
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...

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.