In Part 2 of this series we expanded our file-searching script to be able to search for multiple file extensions under a tree, and to write the results (all paths to files of matching extensions found) to a log file. Now that we've come to the final part of the series, we'll add more functionality (in the form of functions) to our script to be able to move, copy, and even delete the results of the search.
Before looking at the move/copy/delete functions, we'll first take the subroutine of logging the results to file and encapsulate that in a function also. The following is what that part of our script looked like before:
found
changes to results
and logname
to logpath
):move
function from the shutil
module (imagine that :P), so we'll want to add this statement to the beginning of our script:found
variable in the script, we'll have our method take in a dictionary of
results and act on it. It also needs the path to the directory to move
them to and an error log path, if we want it. It will also need a
variable to store the errors (a list of path strings to files):move
function—firstly, this function will move the source argument to the
destination of the same type. This means that if the source path is a
directory, the destination will also be a directory, and likewise for
files. Secondly, if the destination exists and is a file, then the
source must be a file or the function will fail. In other
words, if the destination is a directory, the source (either file or
directory) will be moved into the destination directory, but if the
destination is a file, the source may only be a file (we can't move a
directory into a file). With that said, all we need do is make sure the dest
argument to batchmove
is an existing directory, so we'll use a try statement after testing:results
and moving each file. Here is the loop:results
are simply the file extensions searched for, so only the values are needed, and for each path
in the current paths
list in the values, we try to move it to our destination. If the move fails, the path
is added to the error list. When the loop completes, a message is printed to the standard output.After the loop completes, we'll want to log any errors encountered with our
logerr
method like so:batchmove
function looks like:batchmove
function, in order to define the batchcopy
function, we need only change the function call of the innermost loop to copy2
(and messages accordingly), so the full definition would look like this:dest
check from batchmove
and change the inner loop function call to os.remove
like so:batchmove
and
delete them from there, but of course the choice is up to you :). Now
that we've defined these functions, all we'd need to do to utilize them
is call them after the search loop with found
as the results
argument, and whatever paths to log files we want accordingly, so
finding and moving our files around will be a breeze even if we don't
know where they are!
No comments:
Post a Comment