Error copying files
Question: I'm trying to write a short Python script that will copy all files from a
directory with a certain extension and place them in a different folder. Here
is the script:

    
    
    import os, shutil
    
    source = "C:\\TCR_Calgary\\projects\\"
    
    destination = "C:\\TCR_Calgary\\reference\\my_code\\city_of_calgary_scripts\\"
    
    extension = ".py"
    
    for root, dirs, files in os.walk(source):
        for file in files:
            if file.lower().endswith(extension):
                shutil.copy2(file, destination)
    

This is the exception I get:

    
    
    Traceback (most recent call last):
      File "C:/TCR_Calgary/reference/my_code/city_of_calgary_scripts/python_script_copier.py", line 13, in <module>
        shutil.copy2(file, destination)
      File "C:\Program Files (x86)\Python26\lib\shutil.py", line 99, in copy2
        copyfile(src, dst)
      File "C:\Program Files (x86)\Python26\lib\shutil.py", line 47, in copyfile
        raise Error, "`%s` and `%s` are the same file" % (src, dst)
    shutil.Error: `AnnoMover.py` and `C:\TCR_Calgary\reference\my_code\city_of_calgary_scripts\AnnoMover.py` are the same file
    

I'm confused because AnnoMover.py is not in the destination folder. In my
mind, the original file and its copy would be "the same" although their paths
would not. There's clearly something I'm not understanding here. Any help is
much appreciated!

Version: Python 2.6 Interpeter: PyCharm Community Edition 3.4 OS: Windows 7


Answer: The exception message could be a little clearer, but the last line should be
`shutil.copy2(os.path.join(source, file), destination)` so that the source is
fully qualified.

