By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

search for any number of files and extract their names

P: 14
hi!
i have a directory structure(can be of any type)
I want to search for the files present in it and extract the names of those files(file can be of any format e.g .txt/.c/.py)
How can this be done.I have to search the whole directory structure(including the subdirectories for any files present and if any ,i want the name of all the files)
please help me out.
Dec 28 '06 #1
Share this Question
Share on Google+
6 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
hi!
i have a directory structure(can be of any type)
I want to search for the files present in it and extract the names of those files(file can be of any format e.g .txt/.c/.py)
How can this be done.I have to search the whole directory structure(including the subdirectories for any files present and if any ,i want the name of all the files)
please help me out.
We had a thread similar to this recently. If I understand your question correctly, the following code will do the job:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. if 'C:\\SDS2_7.0\\macro' not in sys.path:
  3.     sys.path.append('C:\\SDS2_7.0\\macro')
  4.  
  5. import os
  6.  
  7. def dir_list2(dir_name):
  8.     fileList = []
  9.     for file in os.listdir(dir_name):
  10.         dirfile = os.path.join(dir_name, file)
  11.         if os.path.isfile(dirfile):
  12.             fileList.append(dirfile)
  13.         elif os.path.isdir(dirfile):
  14.             print "Accessing directory:", dirfile
  15.             fileList += dir_list2(dirfile)
  16.         else:
  17.             pass
  18.     return fileList
  19.  
  20. if __name__ == '__main__':
  21.     dir_name = (os.path.join('C:\\', 'SDS2_7.0', 'macro'))
  22.     fileList = dir_list2(dir_name)
  23.     for f in fileList:
  24.         print f, os.path.getsize(f), os.path.getmtime(f)
'dir_name' is the directory where the search starts. Substitute your start directory.
Dec 28 '06 #2

bvdet
Expert Mod 2.5K+
P: 2,851
To only list files with certain extensions:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. if 'C:\\SDS2_7.0\\macro' not in sys.path:
  3.     sys.path.append('C:\\SDS2_7.0\\macro')
  4.  
  5. import os
  6.  
  7. def dir_list2(dir_name, *args):
  8.     fileList = []
  9.     for file in os.listdir(dir_name):
  10.         dirfile = os.path.join(dir_name, file)
  11.         if os.path.isfile(dirfile):
  12.             if len(args) == 0:
  13.                 fileList.append(dirfile)
  14.             else:
  15.                 if os.path.splitext(dirfile)[1][1:] in args:
  16.                     fileList.append(dirfile)
  17.         elif os.path.isdir(dirfile):
  18.             print "Accessing directory:", dirfile
  19.             fileList += dir_list2(dirfile, *args)
  20.         else:
  21.             pass
  22.     return fileList
  23.  
  24. if __name__ == '__main__':
  25.     dir_name = (os.path.join('C:\\', 'SDS2_7.0', 'macro', 'macrolib'))
  26.     fileList = dir_list2(dir_name, 'py', 'txt')
  27.     for f in fileList:
  28.         print f
To list all files using the above function, simply omit the 'py' and 'txt' arguments in the above function call.
Dec 28 '06 #3

bvdet
Expert Mod 2.5K+
P: 2,851
To only list files with certain extensions:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. if 'C:\\SDS2_7.0\\macro' not in sys.path:
  3.     sys.path.append('C:\\SDS2_7.0\\macro')
  4.  
  5. import os
  6.  
  7. def dir_list2(dir_name, *args):
  8.     fileList = []
  9.     for file in os.listdir(dir_name):
  10.         dirfile = os.path.join(dir_name, file)
  11.         if os.path.isfile(dirfile):
  12.             if len(args) == 0:
  13.                 fileList.append(dirfile)
  14.             else:
  15.                 if os.path.splitext(dirfile)[1][1:] in args:
  16.                     fileList.append(dirfile)
  17.         elif os.path.isdir(dirfile):
  18.             print "Accessing directory:", dirfile
  19.             fileList += dir_list2(dirfile, *args)
  20.         else:
  21.             pass
  22.     return fileList
  23.  
  24. if __name__ == '__main__':
  25.     dir_name = (os.path.join('C:\\', 'SDS2_7.0', 'macro', 'macrolib'))
  26.     fileList = dir_list2(dir_name, 'py', 'txt')
  27.     for f in fileList:
  28.         print f
To list all files using the above function, simply omit the 'py' and 'txt' arguments in the above function call.
By mistake I included some code that does not belong in my previous two posts:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. if 'C:\\SDS2_7.0\\macro' not in sys.path:
  3.     sys.path.append('C:\\SDS2_7.0\\macro')
should not be there! Sorry for the confusion.
Dec 28 '06 #4

Expert 100+
P: 511
you can use the os.walk() module

an example only:
Expand|Select|Wrap|Line Numbers
  1. for root,files,dir in os.walk("yourdir"):
  2.      print root, files,dir
  3.  
to get filenames, you can use os.path.split() or os.path.splitext() depending on your requirement. Check the docs for usage
Dec 29 '06 #5

bvdet
Expert Mod 2.5K+
P: 2,851
For the exercise, here is another version that returns one string:
Expand|Select|Wrap|Line Numbers
  1. import os
  2. from time import sleep
  3.  
  4. def dir_list2(dir_name, *args):
  5.     fileStr = ""
  6.  
  7.     for file in os.listdir(dir_name):
  8.         dirfile = os.path.join(dir_name, file)
  9.  
  10.         if os.path.isfile(dirfile):
  11.  
  12.             if len(args) == 0:
  13.                 fileStr += dirfile + '\n'
  14.             else:
  15.                 if os.path.splitext(dirfile)[1][1:] in args:
  16.                     fileStr += dirfile + '\n'
  17.  
  18.         elif os.path.isdir(dirfile):
  19.             print "Accessing directory:", dirfile
  20.             fileStr += dir_list2(dirfile, *args)
  21.  
  22.     return fileStr
  23.  
  24. if __name__ == '__main__':
  25.     dir_name = (os.path.join('C:\\', 'SDS2_7.0', 'macro'))
  26.     f = dir_list2(dir_name, 'txt')
  27.     print f
  28.  
  29.     fList = f.split('\n')
  30.     sleep(2)
  31.     for s in fList:
  32.         print s
Dec 29 '06 #6

bvdet
Expert Mod 2.5K+
P: 2,851
you can use the os.walk() module

an example only:
Expand|Select|Wrap|Line Numbers
  1. for root,files,dir in os.walk("yourdir"):
  2.      print root, files,dir
  3.  
to get filenames, you can use os.path.split() or os.path.splitext() depending on your requirement. Check the docs for usage
More exercise using os.walk().
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. dir_name = (os.path.join('C:/', 'SDS2_7.0', 'macro'))
  4.  
  5. a = os.walk(dir_name)
  6.  
  7. for root, dir, file in os.walk(dir_name):
  8.  
  9.     print "Root directory: %s" % (root)
  10.  
  11.     if len(dir) > 0:
  12.         print "Subdirectories under %s:" % (root)
  13.         dirList = map(lambda x: '%s\n' % (x), dir)
  14.         dirStr = "".join(dirList)
  15.         print dirStr
  16.     else:
  17.         print "There are no subdirectories under directory %s" % (root)
  18.  
  19.     if len(file) > 0:
  20.         print "Files in directory %s:" % (root)
  21.         fileList = map(lambda x: '%s\n' % (os.path.join(root, x)), file)
  22.         fileStr = "".join(fileList)
  23.         print fileStr
  24.     else:
  25.         print "There are no files in directory %s" % (root)
Thanks ghostdog74.
Jan 1 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.