count files in a directory

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

    count files in a directory

    I assume that there's a better way than this to count the files in a
    directory recursively. Is there???

    def count_em(valid_ path):
    x = 0
    for root, dirs, files in os.walk(valid_p ath):
    for f in files:
    x = x+1
    print "There are", x, "files in this directory."
    return x

    rbt
  • James Stroud

    #2
    Re: count files in a directory

    On Friday 20 May 2005 07:12 pm, rbt wrote:[color=blue]
    > I assume that there's a better way than this to count the files in a
    > directory recursively. Is there???
    >
    > def count_em(valid_ path):
    > x = 0
    > for root, dirs, files in os.walk(valid_p ath):
    > for f in files:
    > x = x+1
    > print "There are", x, "files in this directory."
    > return x
    >
    > rbt[/color]

    def count_em(valid_ path):
    root, dirs, files = os.walk(valid_p ath)
    return len(files)



    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095


    Comment

    • James Stroud

      #3
      Re: count files in a directory


      Come to think of it

      file_count = len(os.walk(val id_path)[2])

      --
      James Stroud
      UCLA-DOE Institute for Genomics and Proteomics
      Box 951570
      Los Angeles, CA 90095


      Comment

      • Scott David Daniels

        #4
        Re: count files in a directory

        James Stroud wrote:[color=blue]
        > Come to think of it
        >
        > file_count = len(os.walk(val id_path)[2])
        >
        > --
        > James Stroud
        > UCLA-DOE Institute for Genomics and Proteomics
        > Box 951570
        > Los Angeles, CA 90095
        >
        > http://www.jamesstroud.com/[/color]

        Somee possible answers are:

        # files directly in path
        file_count = len(os.walk(pat h)[2])

        # files and dirs directly in path
        file_count = len(os.listdir( path))

        # files in or below path
        file_count = 0
        for root, dirs, files in os.walk(path):
        file_count += len(files)


        # files and dirs in or below path
        file_count = 0
        for root, dirs, files in os.walk(path):
        file_count += len(files) + len(dirs)


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

        Comment

        • rbt

          #5
          Re: count files in a directory

          James Stroud wrote:[color=blue]
          > Come to think of it
          >
          > file_count = len(os.walk(val id_path)[2])[/color]

          I get this traceback:

          PythonWin 2.4 (#60, Nov 30 2004, 09:34:21) [MSC v.1310 32 bit (Intel)] on win32.
          Portions Copyright 1994-2004 Mark Hammond (mhammond@skipp inet.com.au) - see
          'Help/About PythonWin' for further copyright information.
          Traceback (most recent call last):
          File "C:\Program
          Files\Python24\ Lib\site-packages\python win\pywin\frame work\scriptutil s.py", line 310,
          in RunScript
          exec codeObject in __main__.__dict __
          File "C:\Documen ts and Settings\rbt\De sktop\newa\repl icate.py", line 57, in ?
          A = count_em(X)
          File "C:\Documen ts and Settings\rbt\De sktop\newa\repl icate.py", line 51, in count_em
          count = len(os.walk(val id_path)[2])
          TypeError: unsubscriptable object

          Comment

          • rbt

            #6
            Re: count files in a directory

            James Stroud wrote:[color=blue]
            > def count_em(valid_ path):
            > root, dirs, files = os.walk(valid_p ath)
            > return len(files)[/color]

            Here's another Tback:
            [color=blue][color=green][color=darkred]
            >>> Traceback (most recent call last):[/color][/color][/color]
            File "C:\Program
            Files\Python24\ Lib\site-packages\python win\pywin\frame work\scriptutil s.py", line 310,
            in RunScript
            exec codeObject in __main__.__dict __
            File "C:\Documen ts and Settings\rbt\De sktop\newa\repl icate.py", line 62, in ?
            A = count_em(X)
            File "C:\Documen ts and Settings\rbt\De sktop\newa\repl icate.py", line 56, in count_em
            root, dirs, files = os.walk(valid_p ath)
            ValueError: need more than 2 values to unpack

            Comment

            • James Stroud

              #7
              Re: count files in a directory

              Sorry, I've never used os.walk and didn't realize that it is a generator.

              This will work for your purposes (and seems pretty fast compared to the
              alternative):

              file_count = len(os.walk(val id_path).next()[2])


              The alternative is:


              import os
              import os.path

              file_count = len([f for f in os.listdir('.') if os.path.isfile( f)])


              On Friday 20 May 2005 08:08 pm, rbt wrote:[color=blue]
              > James Stroud wrote:[color=green]
              > > def count_em(valid_ path):
              > > root, dirs, files = os.walk(valid_p ath)
              > > return len(files)[/color]
              >
              > Here's another Tback:[color=green][color=darkred]
              > >>> Traceback (most recent call last):[/color][/color]
              >
              > File "C:\Program
              > Files\Python24\ Lib\site-packages\python win\pywin\frame work\scriptutil s.py",
              > line 310, in RunScript
              > exec codeObject in __main__.__dict __
              > File "C:\Documen ts and Settings\rbt\De sktop\newa\repl icate.py", line 62,
              > in ? A = count_em(X)
              > File "C:\Documen ts and Settings\rbt\De sktop\newa\repl icate.py", line 56,
              > in count_em root, dirs, files = os.walk(valid_p ath)
              > ValueError: need more than 2 values to unpack[/color]

              --
              James Stroud
              UCLA-DOE Institute for Genomics and Proteomics
              Box 951570
              Los Angeles, CA 90095


              Comment

              • Heiko Wundram

                #8
                Re: count files in a directory

                Am Samstag, 21. Mai 2005 06:25 schrieb James Stroud:[color=blue]
                > This will work for your purposes (and seems pretty fast compared to the
                > alternative):
                >
                > file_count = len(os.walk(val id_path).next()[2])[/color]

                But will only work when you're just scanning a single directory with no
                subdirectories. ..!

                The alternative (which will work regardless of subdirectories) is something
                like the following:

                heiko@heiko ~ $ python
                Python 2.4 (#1, Apr 3 2005, 00:49:51)
                [GCC 3.4.3-20050110 (Gentoo Linux 3.4.3.20050110-r1, ssp-3.4.3.20050110-0,
                pie- on linux2
                Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
                >>> import os
                >>> path = "/home/heiko"
                >>> file_count = sum((len(f) for _, _, f in os.walk(path)))
                >>> file_count[/color][/color][/color]
                55579[color=blue][color=green][color=darkred]
                >>>[/color][/color][/color]

                HTH!

                --
                --- Heiko.
                see you at: http://www.stud.mh-hannover.de/~hwundram/wordpress/

                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.4.1 (GNU/Linux)

                iD8DBQBCjvUvf0b pgh6uVAMRAuuAAJ wOwIgb9Ir3pTZex 7dqc65FiOpd9QCf TA+x
                FHMoRzqJiL6cVP9 n6NwmpkM=
                =s6qV
                -----END PGP SIGNATURE-----

                Comment

                • rbt

                  #9
                  Re: count files in a directory

                  Heiko Wundram wrote:[color=blue]
                  > Am Samstag, 21. Mai 2005 06:25 schrieb James Stroud:
                  >[color=green]
                  >>This will work for your purposes (and seems pretty fast compared to the
                  >>alternative ):
                  >>
                  >>file_count = len(os.walk(val id_path).next()[2])[/color]
                  >
                  >
                  > But will only work when you're just scanning a single directory with no
                  > subdirectories. ..!
                  >
                  > The alternative (which will work regardless of subdirectories) is something
                  > like the following:
                  >
                  > heiko@heiko ~ $ python
                  > Python 2.4 (#1, Apr 3 2005, 00:49:51)
                  > [GCC 3.4.3-20050110 (Gentoo Linux 3.4.3.20050110-r1, ssp-3.4.3.20050110-0,
                  > pie- on linux2
                  > Type "help", "copyright" , "credits" or "license" for more information.
                  >[color=green][color=darkred]
                  >>>>import os
                  >>>>path = "/home/heiko"
                  >>>>file_coun t = sum((len(f) for _, _, f in os.walk(path)))
                  >>>>file_coun t[/color][/color]
                  >
                  > 55579
                  >
                  >
                  > HTH!
                  >[/color]

                  Thanks! that works great... is there any significance to the underscores that you
                  used? I've always used root, dirs, files when using os.walk() do the underscores make
                  it faster... or more efficient?

                  Comment

                  • rbt

                    #10
                    Re: count files in a directory

                    James Stroud wrote:[color=blue]
                    > Sorry, I've never used os.walk and didn't realize that it is a generator.
                    >
                    > This will work for your purposes (and seems pretty fast compared to the
                    > alternative):
                    >
                    > file_count = len(os.walk(val id_path).next()[2])[/color]

                    Thanks James... this works *really* well for times when I only need to count files in
                    the current directory (no recursion). I think others will find it useful as well.

                    Comment

                    • Steven Bethard

                      #11
                      Re: count files in a directory

                      rbt wrote:[color=blue]
                      > Heiko Wundram wrote:[color=green][color=darkred]
                      >>>>> import os
                      >>>>> path = "/home/heiko"
                      >>>>> file_count = sum((len(f) for _, _, f in os.walk(path)))
                      >>>>> file_count[/color][/color]
                      >
                      > Thanks! that works great... is there any significance to the underscores
                      > that you used? I've always used root, dirs, files when using os.walk()
                      > do the underscores make it faster... or more efficient?[/color]

                      No, they don't make any semantic difference; they work just like any
                      other identifier. It's just that, by convention, single underscores
                      indicate that we don't care what values these names are bound to. So in
                      the example above, Heiko indicates that we don't care about what you
                      would normally call 'root' and 'dirs'.

                      HTH,

                      STeVe

                      Comment

                      Working...