Getting all the *files* from a directory -- A better way??

nanotech at europa.com nanotech at europa.com
Wed Mar 28 23:47:56 CEST 2001


Thanks to everyone who answered!

Notice that:

  dir="/a/b/c"

  files1=[f for f in os.listdir(dir)
            if os.path.isfile(f)]
  files2=[os.path.basename(f) for f in os.listdir(dir)
                              if os.path.isfile(f)]

would be much "prettier" than:

  files1=[os.path.join(dir,f) for f in os.listdir(dir)
                              if os.path.isfile(os.path.join(dir,f))]
  files2=[f for f in os.listdir(dir)
            if os.path.isfile(os.path.join(dir,f))]

if os.listdir returned the given path prepended -- but oh well!!

Quentin Crain






More information about the Python-list mailing list