Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 8th, 2008, 02:48 PM
Mague's Avatar
Familiar Sight
 
Join Date: May 2007
Location: Australia. - Can't get in more detail (rapist)
Age: 15
Posts: 138
Default 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
Reply
  #2  
Old September 8th, 2008, 06:28 PM
bvdet's Avatar
Expert
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,214
Default

Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. dir_name = r'C:\XXX'
  4. ext = '.txt'
  5.  
  6. for f in os.listdir(dir_name):
  7.     dirfile = os.path.join(dir_name, f)
  8.     if os.path.isfile(dirfile):
  9.         if os.path.splitext(dirfile)[1] == ext:
  10.             print dirfile
Also:
Expand|Select|Wrap|Line Numbers
  1. import glob
  2. glob.glob1(r'D:\XXX', '*.txt')
Reply
  #3  
Old September 9th, 2008, 05:58 AM
Mague's Avatar
Familiar Sight
 
Join Date: May 2007
Location: Australia. - Can't get in more detail (rapist)
Age: 15
Posts: 138
Default

thanks heaps,

This did the trick

Thanks alot
Mague

ps. Thanks for also being so speedy
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles