AnsweredAssumed Answered

Weekly Folder & File transfer Issues in Python.

Question asked by robertmoore_AMIG on Oct 5, 2020
Latest reply on Oct 5, 2020 by danretired

Hi All:

 

I need to run a weekly python script that will copy (from and existing folder) folders and their contents to another folder in task scheduler, with the caviat that only new data is copied and pasted since the last run. Please help. The code below only moves the folders, but all data inside the folders is deleted.

 

Thanks:

Robert Moore

 

import os

# -- TO DO: Please insert your personal directory paths.
# -- Definition of source (Directory) and target folder (Directory_Archive) paths ----------------------------------------------------------------------
src_path = r'\\PATH-Archive_TestFolder'
dst_path = r'\\PATH-Archive_TestFolder_x'


# -- Definition of copytree method - src: source, dst: destination ---------------------------------------------------
def copytree(src, dst):
dir_content_list = os.listdir(src)
# create destination path if not existing
if not os.path.isdir(dst):
os.mkdir(dst)
# start iteration for updating the destination folder (dir_content: every recognized 'object' in dir_content_list)
for dir_content in dir_content_list:
# creation of comparable directory paths (source and destination) including objects (= dir_content)
src_name = os.path.join(src, dir_content)
dst_name = os.path.join(dst, dir_content)
# start copytree method with input parameters src_name and dst_name incl. error handling
try:
copytree(src_name, dst_name)
except (IOError, os.error), why:
print "Can't copy %s to %s: %s" % (`src_name`, `dst_name`, str(why))


# -- Call copytree method with source and target paths -----------------------------------------------------------------
copytree(src_path, dst_path)

Outcomes