Search by Filename

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mague
    New Member
    • May 2007
    • 137

    Search by Filename

    G'day.

    I would like to search a directory for files with the extension .txt.

    Any idea's?

    Thanks in advance
    Mague
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Code:
    import os
    
    dir_name = r'C:\XXX'
    ext = '.txt'
    
    for f in os.listdir(dir_name):
        dirfile = os.path.join(dir_name, f)
        if os.path.isfile(dirfile):
            if os.path.splitext(dirfile)[1] == ext:
                print dirfile
    Also:
    Code:
    import glob
    glob.glob1(r'D:\XXX', '*.txt')

    Comment

    • Mague
      New Member
      • May 2007
      • 137

      #3
      thanks heaps,

      This did the trick

      Thanks alot
      Mague

      ps. Thanks for also being so speedy

      Comment

      Working...