Organizing function calls once files have been moved to a directory
kbtyo
ahlusar.ahluwalia at gmail.com
Tue Jun 23 16:16:09 EDT 2015
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.
More information about the Python-list
mailing list