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

Importing file names, etc., etc.

P: 2
Hello. I am new to Python. I've just installed version 3.0.1, which I will be using on a Linux box (which I am also new to), and need to use it to essentially call on several files of a specific type within a directory and run an external program to convert them to a different type.

So far I have figured out how to list files in a directory using os.listdir, but I was wondering, is there a way that I could list specific file types? For example: I am trying to specifically list only files with the .sac extension.

After I find all of these .sac files, I need to run a program to convert each file individually into .seed files. To do this I believe I will need to create a loop that cycles through each file name, running it through the external program, and ending once it has run through all of the files.

I am extremely lost, since I have very little programming experience. Any help would be highly appreciated. Thank you in advance.
Apr 6 '09 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 2.5K+
P: 2,851
I use the following function to return a list of file names with the full path.
Expand|Select|Wrap|Line Numbers
  1. import os
  3. def dirEntries(dir_name, subdir, *args):
  4.     '''Return a list of file names found in directory 'dir_name'
  5.     If 'subdir' is True, recursively access subdirectories under 'dir_name'.
  6.     Additional arguments, if any, are file extensions to match filenames.
  7.         Matched file names are added to the list.
  8.     If there are no additional arguments, all files found in the directory
  9.         are added to the list.
  10.     Example usage: fileList = dirEntries(r'H:\TEMP', False, 'txt', 'py')
  11.         Only files with 'txt' and 'py' extensions will be added to the list.
  12.     Example usage: fileList = dirEntries(r'H:\TEMP', True)
  13.         All files and all the files in subdirectories under H:\TEMP will be
  14.         added to the list.
  15.     '''
  16.     fileList = []
  17.     for file in os.listdir(dir_name):
  18.         dirfile = os.path.join(dir_name, file)
  19.         if os.path.isfile(dirfile):
  20.             if not args:
  21.                 fileList.append(dirfile)
  22.             else:
  23.                 if os.path.splitext(dirfile)[1][1:] in args:
  24.                     fileList.append(dirfile)
  25.         # recursively access file names in subdirectories
  26.         elif os.path.isdir(dirfile) and subdir:
  27.             print "Accessing directory:", dirfile
  28.             fileList.extend(dirEntries(dirfile, subdir, *args))
  29.     return fileList
You can pass 'sac' as an argument to return only files with that extension.
Expand|Select|Wrap|Line Numbers
  1. dirEntries(dir_name, False, 'sac')
The False parameter prevents recursion into subdirectories.
Apr 6 '09 #2

P: 2
Thank you very much! I'll be sure to mull this over and it should help me with exactly what I need to do.
Apr 6 '09 #3

Post your reply

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