Python Forum
Cannot copy file to another location
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot copy file to another location
#1
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)
Reply
#2
for shutil.copyfile you specify the source and destination FILE NAMEs. Your command is trying to replace the PED directory with some file.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 239 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 419 Feb-07-2024, 12:24 PM
Last Post: Viento
  is it possible to copy image from email and place into excel file? cubangt 3 1,259 Nov-30-2022, 05:11 PM
Last Post: snippsat
  Please help me [copy and paste file from src to dst] midomarc 2 1,001 Nov-24-2022, 10:13 PM
Last Post: midomarc
  Changing file location azizrasul 6 1,281 Sep-28-2022, 01:01 AM
Last Post: azizrasul
  Copy column from one existing excel file to another file mkujawsk 0 5,602 Apr-14-2021, 06:33 PM
Last Post: mkujawsk
  find the header location in a .bin file without reading the whole file at a time SANJIB 0 2,206 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Failing to copy file from a network to a local drive tester_V 4 7,087 Jan-20-2021, 07:40 AM
Last Post: tester_V
  Copy mp3 file multiple times and rename Mar10 4 3,732 Sep-23-2020, 01:09 AM
Last Post: Mar10
  Python Cut/Copy paste file from folder to another folder rdDrp 4 5,029 Aug-19-2020, 12:40 PM
Last Post: rdDrp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020