Python Forum
How to copy a file to another location? Python 2.7 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to copy a file to another location? Python 2.7 (/thread-28434.html)



How to copy a file to another location? Python 2.7 - rcmanu95 - Jul-19-2020

I am trying to copy a file to another location in Python 2.7 and I am getting error all the time saying no such file or directory. The file is actually present because when I do print fname it does print out the correct filename in that for loop. If you know any other way to copy file to a destination location please do tell me.

import os
import shutil

os.chdir("C:\Users\\user1\Documents\PET\prod")
currDir = os.curdir
destDir = 'C:\Users\\user1\Documents\PET'
for dirName, subDirList, fileList in os.walk(currDir):
    for fname in fileList:
        if fname == 'GMT.bot':
            shutil.copyfile(fname, destDir)
        if fname == 'GMB.fluf':
            shutil.copyfile(fname, destDir)



RE: How to copy a file to another location? Python 2.7 - ndc85430 - Jul-19-2020

Why are you using Python 2.7? It reached end of life at the beginning of the year, so you should be using 3.x now.

Per the docs for copyfile, you need to specify the complete path to the file, not just the destination directory.