[perl-python] 20050127 traverse a dir

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xah Lee

    #1

    [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(my dir, myfun, 'somenull')


    ----------------------
    os.path.walk(ba se_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_d ir,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/SpecialPlaneCur ves_dir'
    © def g(s): print "g touched:", s
    © def myfun(dummy, dirr, filess):
    © for child in filess:
    © if '.html' == os.path.splitex t(child)[1] \
    © and os.path.isfile( dirr+'/'+child):
    © g(dirr+child)
    © os.path.walk(my dir, myfun, 3)

    note that os.path.splitex t 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/SpecialPlaneCur ves_dir';
    find(\&wanted, $mydir);
    sub g($){print shift, "\n";}
    sub wanted {
    if ($_ =~/\.html$/ && -T $File::Find::na me) { g $File::Find::na me;}
    $File::Find::na me;
    }

    # 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
    xah@xahlee.org


  • Chris Mattern

    #2
    Re: [perl-python] 20050127 traverse a dir

    Xah Lee wrote:

    <snip>[color=blue]
    >
    > # 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.[/color]

    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?"

    Comment

    • The Flow

      #3
      Re: [perl-python] 20050127 traverse a dir

      Xah Lee,

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

      Comment

      • Timo Virkkala

        #4
        Re: [perl-python] 20050127 traverse a dir

        The Flow wrote:[color=blue]
        > Do you want to be taken seriously?
        > First, stop posting.
        > Second, learn perl.
        > Third, learn python.[/color]

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

        --
        Timo Virkkala

        Comment

        • The Flow

          #5
          Re: [perl-python] 20050127 traverse a dir

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

          --
          The Flow

          Comment

          • Jürgen Exner

            #6
            Re: [perl-python] 20050127 traverse a dir

            Xah Lee wrote:
            [...][color=blue]
            > [long rant about Perl modules snipped]
            >
            > # And because the way it is
            > # written,[/color]

            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


            Comment

            • Terry Reedy

              #7
              Talking to the wind


              "The Flow" <antbyte.The.Fl ow@gmail.com> wrote in message
              news:1106875259 .426213.252030@ z14g2000cwz.goo glegroups.com.. .[color=blue]
              > Xah Lee,
              >
              > Do you want to be taken seriously?
              > First, stop posting.
              > Second, learn perl.
              > Third, learn python.[/color]

              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



              Comment

              • Dan Perl

                #8
                Re: Talking to the wind


                "Terry Reedy" <tjreedy@udel.e du> wrote in message
                news:mailman.14 81.1106882832.2 2381.python-list@python.org ...[color=blue]
                >
                > 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[/color]

                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


                Comment

                • Abigail

                  #9
                  Re: [perl-python] 20050127 traverse a dir

                  Timo Virkkala (a@a.invalid) wrote on MMMMCLXVIII September MCMXCIII in
                  <URL:news:nGgKd .620$zi5.487@re ad3.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(11 5,f(116,f(32,f( 97,
                  f(110,f(111,f(1 16,f(104,f(0x65 ,f(114,f(32,f(8 0,f(101,f(114,f (0x6c,f(32,
                  f(0x48,f(97,f(9 9,f(107,f(101,f (114,f(10,q ff))))))))))))) ))))))))))))

                  Comment

                  • Xah Lee

                    #10
                    [perl-python] daily tip website

                    for those interested, i've created a webpage for these perl-python
                    daily tips. The url is:


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

                    Xah
                    xah@xahlee.org



                    [color=blue]
                    > Hey all, I have seen no evidence that XL even reads the responses[/color]
                    that have[color=blue]
                    > been directed thereto.
                    > Why don't you ever answer the messages
                    > ...[/color]

                    Comment

                    • Timo Virkkala

                      #11
                      Re: [perl-python] daily tip website

                      Xah Lee wrote:[color=blue]
                      > Thanks to those who have made useful comments. They will be
                      > assimilated.[/color]

                      The comments, or those who have made comments?

                      "Correction s are futile. You will be assimilated."
                      --Xah Lee

                      --
                      Timo Virkkala

                      Comment

                      • Chris Mattern

                        #12
                        Re: [perl-python] daily tip website

                        Xah Lee wrote:
                        [color=blue]
                        > daily tips. The url is:
                        > http://xahlee.org/perl-python/python.html
                        >
                        > Thanks to those who have made useful comments. They will be
                        > assimilated.
                        >[/color]
                        Resistance is futile.

                        --
                        Christopher Mattern

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

                        Comment

                        • Skip Montanaro

                          #13
                          Re: [perl-python] 20050127 traverse a dir


                          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

                          Comment

                          • Abigail

                            #14
                            Re: [perl-python] 20050127 traverse a dir

                            Skip Montanaro (skip@pobox.com ) wrote on MMMMCLXVIII September MCMXCIII
                            in <URL:news:mailm an.1540.1106945 544.22381.pytho n-list@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

                            Comment

                            • David H Wild

                              #15
                              Re: Talking to the wind

                              In article <mailman.1481.1 106882832.22381 .python-list@python.org >,
                              Terry Reedy <tjreedy@udel.e du> wrote:[color=blue][color=green]
                              > > Xah Lee,
                              > >
                              > > Do you want to be taken seriously?
                              > > First, stop posting.
                              > > Second, learn perl.
                              > > Third, learn python.[/color][/color]
                              [color=blue]
                              > Hey all, I have seen no evidence that XL even reads the responses that
                              > have been directed thereto. The above is like[/color]
                              [color=blue]
                              > /dev/null,
                              > Why don't you ever answer the messages I keep sending to you?[/color]

                              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
                              _______________ ____________/ dhwild@argonet. co.uk

                              Comment

                              Working...