Select to view content in your preferred language

Weekly Folder & File transfer Issues in Python.

910
2
10-05-2020 06:00 AM
RobertMoore5
New Contributor

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)

0 Kudos
2 Replies
JoshuaBixby
MVP Esteemed Contributor

with the caviat that only new data is copied and pasted since the last run.

By new, do you mean newly present or data that has been modified too?  Where in your code are you comparing and only copying new?  Or, are you relying on the try/except block?  If so, that answers my first question since you aren't copying files that exist regardless of datetime stamps.

Regarding names for functions, since there is a well established copytree in shutil — High-level file operations — Python 3.8.6 documentation , I would avoid naming your function copytree since most people that see that name will assume it is the well established one.

My guess is that your code is returning WinError 267 a lot, correct?  The line in your code where you are checking if something is a directory or not is the likely problem because a file is not a directory, so the code will attempt to create a directory with the same name as a file that already exists.  That error cascades upward since you are recursively calling your function.

DanPatterson
MVP Esteemed Contributor

/blogs/dan_patterson/2016/08/14/script-formatting 

would help with readability and facilitate referencing line numbers


... sort of retired...