Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93049

Organizing function calls once files have been moved to a directory

Newsgroups comp.lang.python
Date 2015-06-23 13:16 -0700
Message-ID <66a66a08-a5f2-4571-819a-38427c46fcf4@googlegroups.com> (permalink)
Subject Organizing function calls once files have been moved to a directory
From kbtyo <ahlusar.ahluwalia@gmail.com>

Show all headers | View raw


I am working on a workflow module that will allow one to recursively check for file extensions and if there is a match move them to a folder for processing (parsing, data wrangling etc). 

I have a simple search process, and log for the files that are present (see below). However, I am puzzled by what the most efficient method/syntax is to call functions once the selected files have been moved? I have the functions and classes written in another file. Should I import them or should I include them in the same file as the following mini-script?

Moreover, should I create another log file for processing? If so, what is an idiomatically correct method to do so? 

if __name__ == '__main__':

# The top argument for name in files
    topdir = '.'
    dest = 'C:\\Users\\wynsa2\\Desktop\\'
    extens = ['docs', 'docx', 'pdf'] # the extensions to search for     
    found = {x: [] for x in extens} # lists of found files    
 
    # Directories to ignore
    ignore = ['docs', 'doc', 'py', 'pdf']     
    logname = "file_search.log"    
    print('Beginning search for files in %s' % os.path.realpath(topdir))  
  
    # Walk the tree
    for dirpath, dirnames, files in os.walk(topdir):
        # Remove directories in ignore
        # directory names must match exactly!
        for idir in ignore:
            if idir in dirnames:
                dirnames.remove(idir)
     
        # Loop through the file names for the current step
        for name in files:
     #Calling str.rsplit on name then 
    #splits the string into a list (from the right) 
    #with the first argument "."" delimiting it, 
    #and only making as many splits as the second argument (1). 
    #The third part ([-1]) retrieves the last element of the list--we 
    #use this instead of an index of 1 because if no splits are made 
    #(if there is no "."" in name), no IndexError will be raised

            ext = name.lower().rsplit('.', 1)[-1]
     
            # Save the full name if ext matches
            #log_results, errlog and batchcopy are functions
            if ext in extens:
                found[ext].append(os.path.join(dirpath, name))
                log_results(logname, found)
                batchcopy(found, dest, errlog=None)

Thank you for your help. 
              

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Organizing function calls once files have been moved to a directory kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-06-23 13:16 -0700
  Re: Organizing function calls once files have been moved to a directory Steven D'Aprano <steve@pearwood.info> - 2015-06-24 12:18 +1000
    Re: Organizing function calls once files have been moved to a directory kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-06-24 04:59 -0700

csiph-web