[perl-python] 20050127 traverse a dir

Discussion in 'Python' started by Xah Lee, Jan 27, 2005.

  1. Xah Lee

    Xah Lee Guest

    # -*- 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://xahlee.org/PageTwo_dir/more.html
    Xah Lee, Jan 27, 2005
    #1
    1. Advertising

  2. 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?"
    Chris Mattern, Jan 27, 2005
    #2
    1. Advertising

  3. Xah Lee

    The Flow Guest

    Xah Lee,

    Do you want to be taken seriously?
    First, stop posting.
    Second, learn perl.
    Third, learn python.
    The Flow, Jan 28, 2005
    #3
  4. 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
    Timo Virkkala, Jan 28, 2005
    #4
  5. Xah Lee

    The Flow Guest

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

    --
    The Flow
    The Flow, Jan 28, 2005
    #5
  6. 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
    Jürgen Exner, Jan 28, 2005
    #6
  7. Xah Lee

    Terry Reedy Guest

    Talking to the wind

    "The Flow" <> wrote in message
    news:...
    > 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
    Terry Reedy, Jan 28, 2005
    #7
  8. Xah Lee

    Dan Perl Guest

    Re: Talking to the wind

    "Terry Reedy" <> wrote in message
    news:...
    >
    > 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
    Dan Perl, Jan 28, 2005
    #8
  9. Xah Lee

    Abigail Guest

    Timo Virkkala () wrote on MMMMCLXVIII September MCMXCIII in
    <URL:news:nGgKd.620$>:
    @@ 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(101,f(114,f(0x6c,f(32,
    f(0x48,f(97,f(99,f(107,f(101,f(114,f(10,q ff)))))))))))))))))))))))))
    Abigail, Jan 28, 2005
    #9
  10. Xah Lee

    Xah Lee Guest

    [perl-python] daily tip website

    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

    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
    > ...
    Xah Lee, Jan 28, 2005
    #10
  11. Re: [perl-python] daily tip website

    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
    Timo Virkkala, Jan 28, 2005
    #11
  12. Re: [perl-python] daily tip website

    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?"
    Chris Mattern, Jan 28, 2005
    #12
  13. Xah Lee

    David H Wild Guest

    Re: Talking to the wind

    In article <>,
    Terry Reedy <> 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
    ___________________________/
    David H Wild, Jan 28, 2005
    #13
  14. 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
    Skip Montanaro, Jan 28, 2005
    #14
  15. Xah Lee

    Abigail Guest

    Skip Montanaro () wrote on MMMMCLXVIII September MCMXCIII
    in <URL:news:>:
    __
    __ 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
    Abigail, Jan 28, 2005
    #15
  16. 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.)
    Jeremy Bowers, Jan 29, 2005
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UnVkeQ==?=

    Sub Dir, Virtual dir, what do I use?

    =?Utf-8?B?UnVkeQ==?=, Jun 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    422
    =?Utf-8?B?UnVkeQ==?=
    Jun 12, 2005
  2. Matthew Denner
    Replies:
    1
    Views:
    178
  3. Kga Agk
    Replies:
    2
    Views:
    147
    Kga Agk
    Jun 29, 2009
  4. Xah Lee

    [perl-python] 20050127 traverse a dir

    Xah Lee, Jan 27, 2005, in forum: Perl Misc
    Replies:
    10
    Views:
    158
    Jeremy Bowers
    Jan 29, 2005
  5. Nick Gnedin
    Replies:
    2
    Views:
    160
Loading...

Share This Page