Importing file names, etc., etc.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • squirrelknight
    New Member
    • Apr 2009
    • 2

    Importing file names, etc., etc.

    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.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I use the following function to return a list of file names with the full path.
    Code:
    import os
    
    def dirEntries(dir_name, subdir, *args):
        '''Return a list of file names found in directory 'dir_name'
        If 'subdir' is True, recursively access subdirectories under 'dir_name'.
        Additional arguments, if any, are file extensions to match filenames.
            Matched file names are added to the list.
        If there are no additional arguments, all files found in the directory
            are added to the list.
        Example usage: fileList = dirEntries(r'H:\TEMP', False, 'txt', 'py')
            Only files with 'txt' and 'py' extensions will be added to the list.
        Example usage: fileList = dirEntries(r'H:\TEMP', True)
            All files and all the files in subdirectories under H:\TEMP will be
            added to the list.
        '''
        fileList = []
        for file in os.listdir(dir_name):
            dirfile = os.path.join(dir_name, file)
            if os.path.isfile(dirfile):
                if not args:
                    fileList.append(dirfile)
                else:
                    if os.path.splitext(dirfile)[1][1:] in args:
                        fileList.append(dirfile)
            # recursively access file names in subdirectories
            elif os.path.isdir(dirfile) and subdir:
                print "Accessing directory:", dirfile
                fileList.extend(dirEntries(dirfile, subdir, *args))
        return fileList
    You can pass 'sac' as an argument to return only files with that extension.
    Code:
    dirEntries(dir_name, False, 'sac')
    The False parameter prevents recursion into subdirectories.

    Comment

    • squirrelknight
      New Member
      • Apr 2009
      • 2

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

      Comment

      Working...