Hi, I'm new to python. I'm creating python script that will copy file from different destination to another destination. It will copy as well the access for different user. I'm trying to output any results to the logfile but it didn't seem to output it. Here's my code. Hope anyone can help. Thanks.
import os
import shutil
import string
import logging
source = r'C:\\test'
dest = r'Z:\\testing'
# set up logging to file
logger = logging.getLogger(source)
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('logfile.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
header= string.join (("Started : %(asctime)s", "Source : %s"% source, "Destination : %s" % dest), "\r\n")
formatter = logging.Formatter(header)
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
for root, dirs, files in os.walk(source):
desti = dest + root.replace(source, '')
#if we're in a directory that doesn't exist in the destination folder
#then create a new folder
if not os.path.isdir(desti):
os.mkdir(desti)
logging.info('Directory created at: ' % dest)
#loop through all files
for f in files:
#compute current (old) & new file locations
oldLoc = root + '\\' + f
newLoc = desti + '\\' + f
if not os.path.isfile(newLoc):
try:
shutil.copy2(oldLoc, newLoc)
logging.info('File is successfully copied.')
except IOError:
logging.info('File is already exists')
try:
shutil.copymode(oldLoc, newLoc)
logging.info('The file has been copied with access')
except IOError:
logging.debug('The file could not copied with access')