os.walk question

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

    os.walk question

    How would one make a list of the files in the top directory
    using os.walk.

    I need to pick a random file from said list.

    Thanks.



    -- Posted on news://freenews.netfront.net - Complaints to news@netfront.n et --
  • alex23

    #2
    Re: os.walk question

    On Jul 24, 11:52 am, "Lanny" <la...@freshell s.chwrote:
    How would one make a list of the files in the top directory
    using os.walk.
    >
    I need to pick a random file from said list.
    So you -only- want the files from one directory?

    Try: _, _, files = os.walk('/top/folder/here').next()

    The single underscore is a convention to indicate we don't care about
    those results.

    The .next() is needed to step through os.walk once, which will start
    at the path you specify.

    Hope this helps.

    - alex23

    Comment

    • Fredrik Lundh

      #3
      Re: os.walk question

      Lanny wrote:
      How would one make a list of the files in the top directory
      using os.walk.
      >
      I need to pick a random file from said list.
      if you want a list of files from a single directory, use listdir, not walk:
      >>import os, random
      >>random.choice (os.listdir("/"))
      'python25'

      </F>

      Comment

      • alex23

        #4
        Re: os.walk question

        On Jul 23, 5:22 pm, Fredrik Lundh <fred...@python ware.comwrote:
        if you want a list of files from a single directory, use listdir, not walk:
        >
              >>import os, random
              >>random.choice (os.listdir("/"))
              'python25'
        This will include folders as well, though, which isn't what the OP
        asked for.
        >>import os, random
        >>root = "/"
        >>random.choice ([f for f in os.listdir(root ) if os.path.isfile( os.path.join(ro ot, f))])
        'initrd.img'

        It just seems clunky compared to os.walk :)

        Comment

        • Larry Bates

          #5
          Re: os.walk question

          Fredrik Lundh wrote:
          Lanny wrote:
          >
          >How would one make a list of the files in the top directory
          >using os.walk.
          >>
          >I need to pick a random file from said list.
          >
          if you want a list of files from a single directory, use listdir, not walk:
          >
          >>import os, random
          >>random.choice (os.listdir("/"))
          'python25'
          >
          </F>
          >
          Or use glob.

          import glob
          random.choice([f for f in glob.glob(root, "*")])

          -Larry

          Comment

          • Fredrik Lundh

            #6
            Re: os.walk question

            alex23 wrote:
            >if you want a list of files from a single directory, use listdir, not walk:
            >>
            > >>import os, random
            > >>random.choice (os.listdir("/"))
            > 'python25'
            >
            This will include folders as well, though, which isn't what the OP
            asked for.
            as illustrated by my example (cut and pasted from a windows machine). oops.

            random.choice(f ilter(os.path.i sfile, glob.glob("/*")))

            isn't that bad, though.

            </F>

            Comment

            • alex23

              #7
              Re: os.walk question

              On Jul 23, 10:11 pm, Larry Bates <larry.ba...@we bsafe.com`wrote :
              import glob
              random.choice([f for f in glob.glob(root, "*")])
              glob.glob only accepts one argument though, did you mean root + "*"?

              It also returns folders as well as files, though, and the list
              comprehension is not necessary given it returns a list itself.

              Comment

              • alex23

                #8
                Re: os.walk question

                On Jul 23, 10:26 pm, Fredrik Lundh <fred...@python ware.comwrote:
                     random.choice(f ilter(os.path.i sfile, glob.glob("/*")))
                >
                isn't that bad, though.
                I'll agree to that.

                Comment

                • B

                  #9
                  Re: os.walk question

                  Lanny wrote:
                  How would one make a list of the files in the top directory
                  using os.walk.
                  >
                  I need to pick a random file from said list.
                  >
                  Thanks.
                  >
                  >
                  >
                  >
                  -- Posted on news://freenews.netfront.net - Complaints to news@netfront.n et --
                  >
                  how about:

                  import os

                  x = os.walk('/')
                  (root,dirs,file s) = x.next()



                  'files' should contain a list of filenames.

                  Comment

                  • Lawrence D'Oliveiro

                    #10
                    Re: os.walk question

                    In message <mailman.514.12 16797917.922.py thon-list@python.org >, Fredrik
                    Lundh wrote:
                    Lanny wrote:
                    >
                    >How would one make a list of the files in the top directory
                    >using os.walk.
                    >>
                    >I need to pick a random file from said list.
                    >
                    if you want a list of files from a single directory, use listdir, not
                    walk:
                    >
                    >>import os, random
                    >>random.choice (os.listdir("/"))
                    'python25'
                    Won't that return any subdirectories as well as files?

                    Comment

                    • Fredrik Lundh

                      #11
                      Re: os.walk question

                      Lawrence D'Oliveiro wrote:
                      Won't that return any subdirectories as well as files?
                      sure, as was discussed in this very thread two days ago.

                      </F>

                      Comment

                      • Eric Wertman

                        #12
                        Re: os.walk question

                        I do this, mabye a no-no?


                        import os

                        for root,dirs,files in os.walk(dir) : break

                        Comment

                        • Terry Reedy

                          #13
                          Re: os.walk question



                          Eric Wertman wrote:
                          I do this, mabye a no-no?
                          It is a roundabout way to do multiple assignment:
                          import os
                          for root,dirs,files in os.walk(dir) : break
                          root,dirs,files = os.walk(dir).ne xt #2.x
                          root,dirs,files = next(os.walk(di r))#3.x

                          Comment

                          Working...