looping over the files in a directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elaine Jackson

    looping over the files in a directory

    Can anyone please tell me the actual syntax for saying something like this:

    for <file> in <directory>:
    <do something with the file>

    (?) Muchas gracias.

    Peace


  • Mark J. Nenadov

    #2
    Re: looping over the files in a directory

    On Fri, 14 May 2004 15:32:32 +0000, Elaine Jackson wrote:
    [color=blue]
    > Can anyone please tell me the actual syntax for saying something like this:
    >
    > for <file> in <directory>:
    > <do something with the file>[/color]

    A simple example:

    import os

    path = '/home/bob/'

    for f in os.listdir(path ):
    print f

    --
    Mark J. Nenadov
    Python Byte Solutions

    Comment

    • Jeremy Sanders

      #3
      Re: looping over the files in a directory

      On Fri, 14 May 2004 15:32:32 +0000, Elaine Jackson wrote:
      [color=blue]
      > Can anyone please tell me the actual syntax for saying something like this:
      >
      > for <file> in <directory>:
      > <do something with the file>
      >
      > (?) Muchas gracias.[/color]

      There's also glob which may be useful depending on what you're doing, e.g.

      import glob

      for f in glob.glob("*.tx t"):
      print f

      Jeremy

      Comment

      • Scott David Daniels

        #4
        Re: looping over the files in a directory

        Mark J. Nenadov wrote:
        [color=blue]
        > On Fri, 14 May 2004 15:32:32 +0000, Elaine Jackson wrote:
        >[color=green]
        >>Can anyone please tell me the actual syntax for saying something like this:
        >> for <file> in <directory>:
        >> <do something with the file>[/color]
        > A simple example:
        > import os
        > path = '/home/bob/'
        > for f in os.listdir(path ):
        > print f[/color]

        A further clue for Elaine is:
        listdir returns file "names". Files are things the OS has; names are
        how we talk about the files to the OS. If you want to do anything with
        the file, you'll have to decide what you want to do, and communicate the
        _full_name_ of the file to the OS when you try to do it.

        You can do that either by joining the path and file names, or by
        changing the "working directory" so that the simple file name works.
        Which you do depends on your overall goal.

        This is purposely left a bit elliptical to leave you some work in
        case this is a homework assignment.

        --
        -Scott David Daniels
        Scott.Daniels@A cm.Org

        Comment

        • Krzysztof Szynter

          #5
          Re: looping over the files in a directory

          Scott David Daniels <Scott.Daniels@ Acm.Org> wrote in
          news:40a4fba5$1 @nntp0.pdx.net:
          [color=blue]
          > This is purposely left a bit elliptical to leave you some work in
          > case this is a homework assignment.[/color]

          Sorry for that, but what does mean 'a bit elliptical' in this sentence?
          Is it a kind of circumlocution?

          --
          Krzysztof Szynter :'''. :. : *
          Dygi GG 1027078 :...' ..... : : : ..... . . . . . .....
          http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :....
          dygimail(at)poc zta(dot)fm :...' :.... : ': :.... : : :..' : :....

          Comment

          • Elaine Jackson

            #6
            Re: looping over the files in a directory

            elliptical <=> some information is left unspecified

            "Krzysztof Szynter" <dygimailNo@SPA Mpoczta.fm> wrote in message
            news:Xns94E9F12 809260dygimailp ocztafm@127.0.0 .1...
            | Scott David Daniels <Scott.Daniels@ Acm.Org> wrote in
            | news:40a4fba5$1 @nntp0.pdx.net:
            |
            | > This is purposely left a bit elliptical to leave you some work in
            | > case this is a homework assignment.
            |
            | Sorry for that, but what does mean 'a bit elliptical' in this sentence?
            | Is it a kind of circumlocution?
            |
            | --
            | Krzysztof Szynter :'''. :. : *
            | Dygi GG 1027078 :...' ..... : : : ..... . . . . . .....
            | http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :....
            | dygimail(at)poc zta(dot)fm :...' :.... : ': :.... : : :..' : :....


            Comment

            • Elaine Jackson

              #7
              Re: looping over the files in a directory

              Works like a charm. Thanks for the tip.

              "Mark J. Nenadov" <mark@freelan ce-developer.com> wrote in message
              news:pan.2004.0 5.14.16.17.33.2 73871@freelance-developer.com.. .
              | On Fri, 14 May 2004 15:32:32 +0000, Elaine Jackson wrote:
              |
              | > Can anyone please tell me the actual syntax for saying something like this:
              | >
              | > for <file> in <directory>:
              | > <do something with the file>
              |
              | A simple example:
              |
              | import os
              |
              | path = '/home/bob/'
              |
              | for f in os.listdir(path ):
              | print f
              |
              | --
              | Mark J. Nenadov
              | Python Byte Solutions
              | http://www.pythonbyte.com/


              Comment

              • Krzysztof Szynter

                #8
                Re: looping over the files in a directory

                "Elaine Jackson" <elainejackson7 355@home.com> wrote in
                news:uLcpc.4785 56$Ig.443767@pd 7tw2no:
                [color=blue]
                > elliptical <=> some information is left unspecified[/color]

                Thanks a lot.

                --
                Krzysztof Szynter :'''. :. : *
                Dygi GG 1027078 :...' ..... : : : ..... . . . . . .....
                http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :....
                dygimail(at)poc zta(dot)fm :...' :.... : ': :.... : : :..' : :....

                Comment

                • Daniel 'Dang' Griffith

                  #9
                  Re: looping over the files in a directory

                  On Fri, 14 May 2004 15:32:32 GMT, "Elaine Jackson"
                  <elainejackson7 355@home.com> wrote:
                  [color=blue]
                  >Can anyone please tell me the actual syntax for saying something like this:
                  >
                  >for <file> in <directory>:
                  > <do something with the file>
                  >
                  >(?) Muchas gracias.
                  >
                  >Peace[/color]

                  Mark Nenadov posted a solution for a single directory. If you want to
                  do so recursively, try something like this:

                  import os
                  for root, d, files in os.walk("."):
                  for basename in files:
                  filename = "%s/%s" % (root, basename)
                  print filename # or open it, or whatever...

                  --dang

                  Comment

                  Working...