Recursive vs non-recursive file/folder copy functions with optional replacement

Would someone else reading my code wish it wasn't recursive?

I'm writing a function to report file and folder copy errors. For now exceptions are being printed but they will be eventually sent to a database. I tried to refactor the error checking code with recursion, but this feels over engineered.

Recursive

def copy(paths, replace=False):     """ Copy a file or folder with optional replacement      Usage: copy((str source, str destination), bool replace)      If replace is True, the destination files or folders will be deleted, and     items from source will replace them.       The function returns the list 'copied_replaced' which indicates if items     were successfully copied or replaced respectively.         """     copied_replaced = [False, False]     # ensure proper usage and exit the function     if type(paths) is not tuple or type(replace) is not bool:         raise TypeError('Paths must be a tuple, replace must be a bool')     # select functions to delete a file or folder     if paths[1] is None:         file = os.remove         folder = shutil.rmtree     # select functions to copy a file or folder     else:         file = shutil.copy         folder = shutil.copytree     # exception handling     try:         # delete from destination         if replace:             destination = paths[-1]             if os.path.exists(destination):                 copied_replaced = copy((destination, None), replace=False)         # copy from source to destination         if os.path.isfile(paths[0]):             file(*paths)         else:             folder(*paths)     # eventually report exceptions to a database     except OSError as error:         # don't report during second, replacement, call         if not replace and paths[1] is not None:             print('The path exists:', error)         # report for both copy and replacement calls         else:             print("Can't access:", error)     else:         # second, replacement, call         if paths[1] is None:             copied_replaced[1] = True         # first, copy, call         else:             copied_replaced[0] = True             return copied_replaced 

Non-recursive

def copy(source, destination, replace=False):     """ Copy a file or folder with optional replacement      Usage: copy(str source, str destination, bool replace)      If replace is True, the destination files or folders will be deleted, and     items from source will replace them.       The function returns the variables 'copied', and 'replaced' which indicate     if items were successfully copied or replaced respectively.         """         # check usage     usage = [('source', str), ('destination', str), ('replace', bool)]     for i, argument in enumerate([source, destination, replace]):         arg, arg_type = usage[i]         if not isinstance(argument, arg_type):             raise TypeError('{} must be {}'.format(arg, arg_type))     # remove items in the destination     if replace:         args = (os.remove, shutil.rmtree, source, [destination])         replaced = try_copy(*args)     # don't replace items in the destination     else:         replaced = False         try:             # don't try to copy if destination exists             if os.path.exists(destination):                 # return False for 'copied' and 'replaced'                 return (False, False)         except Exception as error:             print("Can't access:", error)             # return False for 'copied' and 'replaced'             return (False, False)     # copy items from source to destination     args = (shutil.copy, shutil.copytree, source, [source, destination])     copied = try_copy(*args)     return (copied, replaced)  def try_copy(file, folder, source, paths):     """ Exception handling for the copy function """     # assume operation didn't succeed      success = False     try:         if os.path.isfile(source):             file(*paths)         elif os.path.isdir(source):             folder(*paths)     except Exception as error:         print('Error:', error)     else:         success = True     return success        

Replay

Category: python Time: 2016-07-30 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.204 (s). 12 q(s)