Search code examples
pythondirectoryshutilfile-copying

New to Python using shutil cant find existing filepath


I'm new to python. I'm trying to create a simple program that uses shutil to copy a folder within the C drive to another folder. When copied I'd like to add the date/time to the end of the folder name in its new location. This is used for file back up and cleanup. Here is what I've got so far but it cant find the file path. Eventually this has to save the copied files from a local PC to a network drive.

import shutil
import datetime
import os

SOURCE = "C:/Program Files(x86) /FOLDER1/LOGS"
AppendDate=datetime.datetime.now()
BACKUP = "C:/Users/ME/Desktop/FOLDERNEW/LOGS %s" % AppendDate

shutil.copytree(SOURCE,BACKUP)
print os.listdir(BACKUP)

Solution

  • Sorry for the post because I can't 'comment' yet.

    However, it looks like the error might be a single space omitted from your program file (x86) line.

    SOURCE = "C:/Program Files (x86)/"
    

    As for the Date, it will have to be appended in a format that windows except for naming folders.

    Best practices for file naming

    For this replace Appenddate characters using replace.

    import re
    
    cleandate = re.sub('[-!@#$:.]', '_', str(AppendDate))
    

    This worked for me.

    import shutil
    import datetime
    import os
    import re
    
    
    
    SOURCE = "C:/Program Files (x86)/HP"
    AppendDate=datetime.datetime.now()
    cleandate = re.sub('[-!@#$:.]', '_', str(AppendDate))
    BACKUP = "C:/Users/Robert/Desktop/FOLDERNEW/LOGS %s" % cleandate
    
    shutil.copytree(SOURCE,BACKUP)
    print os.listdir(BACKUP)