import shutil
from DataCleaning.library.functions.changeDirectory import *
import logging
import datetime
def copy2Output(outputFileName, storagePath, targetPath, rm = False, printLog = True):
start = datetime.datetime.now()
print("uploading: " + outputFileName)
logging.info("uploading: " + outputFileName)
if rm:
task = ["remove", "removing", "removed"]
else:
task = ["copy", "copying", "copied"]
tempInitMsg = task[1] + " " + outputFileName + " from " + storagePath + " to " + targetPath
if printLog:
print(tempInitMsg)
logging.info(tempInitMsg)
tempWd = os.getcwd()
os.chdir(storagePath)
try:
if rm:
shutil.move(outputFileName, targetPath)
else:
shutil.copy(outputFileName, targetPath + "\\" + outputFileName)
tempCompleteMsg = task[2] + " " + outputFileName + " from " + storagePath + " to " + targetPath
if printLog:
print(tempCompleteMsg)
logging.info(tempCompleteMsg)
except Exception as error:
tempErrorMsg = "error occurred: file either does not exists in: \n" + storagePath + "\nor already exists in: \n" + targetPath
if printLog:
print(tempErrorMsg)
print(error)
logging.info(tempErrorMsg)
os.chdir(tempWd)
print("successfully uploaded: " + outputFileName)
logging.info("successfully uploaded: " + outputFileName)
end = datetime.datetime.now()
tempTimeMsg = "time spent on copying/moving: " + str(end - start)
if printLog:
print(tempTimeMsg)
logging.info(tempTimeMsg)