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')

Recommended Answers

All 9 Replies

I think logging.info() should be logger.info().

commented: still no output +0

Here is a code which woks for me

import os
import shutil
import string
import logging
pjoin = os.path.join

source = '/home/eric/Documents/daniweb/480781/test' # pjoin('C:', 'test')
dest = '/home/eric/Documents/daniweb/480781/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.", "Source: %s"% source,"Destination : %s" % dest), "\r\n")
formatter = logging.Formatter("%(asctime)s %(message)s")
fh.setFormatter(formatter) 
ch.setFormatter(formatter)

# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
_debug = logger.debug
_info = logger.info
_info(header)


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)
        _info('Directory created at: %s' % desti)

    #loop through all files
    for f in files:

        #compute current (old) & new file locations
        oldLoc = pjoin(root, f)
        newLoc = pjoin(desti, f)

        if not os.path.isfile(newLoc):
            try:
                shutil.copy2(oldLoc, newLoc)                               
                _info('File is successfully copied: %s' % newLoc)

            except IOError:
               _info('File is already exists: %s' % newLoc)

            try:    
                 shutil.copymode(oldLoc, newLoc)
                 _info('The file has been copied with access: %s' % newLoc)

            except IOError:
                _debug('The file could not copied with access: %s' % newLoc)

Thanks. Btw, do you know how to copy users from active directory to another computer?

Btw, do you know how to copy users from active directory to another computer?

No I don't know. It looks like a question about the windows OS rather than the python language. You could start a new thread in the windows forum.

okay. How to configure the logfile to save into specific directory? and how can I rewrite the file?

Why not

fh = logging.FileHandler(
    pjoin('specific','directory','logfile.log'),
    mode = 'w',)

?

the file couldn't rewrite. It will append the data. what should I do?

Can you post a short code with mode = 'w', which doesn't rewrite the file ?

Hi, thanks for your help. I just code as write to file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.