-1

im trying to copy a .txt file from one dest to another. This code is running but the file isnt copying. What am i doing wrong?

import shutil
import os
src = "/c:/users/mick/temp"
src2 = "c:/users/mick"
dst = "/c:/users/mick/newfolder1/"

for files in src and src2:
    if files.endswith(".txt"):
        shutil.copy(files, dst)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
Mick Burton
  • 1
  • 1
  • 2

4 Answers4

2

Your for loop isn't actually searching through the files of each of your sources. In addition your for loop isn't looping through each of the sources but rather the letters in src and src2 that are present in both. This adjustment should handle what you need.

import os
src = ["c:/users/mick/temp", "c:/users/mick"]
dst = "c:/users/mick/newfolder1/"

for source in src:
    for src_file in os.listdir(source):
        if src_file.endswith(".txt"):
            old_path = os.path.join(source, src_file)
            new_path = os.path.join(dst, src_file)
            os.rename(old_path, new_path)

You shouldn't need shutil for this situation as it is simply a more powerful os.rename that attempts to handle different scenarios a little better (to my knowledge). However if "newfolder1" isn't already existant than you will want to replace os.rename() with os.renames() as this attempts to create the directories inbetween.

  • hmm isee what your saying. Although now i am getting an error. "file" not defined" – Mick Burton Sep 09 '14 at 17:57
  • Try my edit, might have possibly conflicted with a built-in "file" namespace. If that edit doesn't work, please comment with a full traceback so I can better solve the issue. –  Sep 09 '14 at 18:00
  • k now when i run yours im getting an error on line 10 os.renam(old_path, new_path)...[error3] the system cannot find the path specified. (sry im a little new to coding and im not quite sure how to copy paste the powershell window i.e traceback) – Mick Burton Sep 09 '14 at 18:05
1

shutils is useful for copying, but to move a file use

os.rename(oldpath, newpath)
khelwood
  • 55,782
  • 14
  • 81
  • 108
1

I know the question is little older. But Here are the simple way to achieve your task. You may change the file extension as you wish. For copying you may use copy() & for moving yo may change it to move(). Hope this helps.

#To import the modules
import os
import shutil


#File path list atleast source & destination folders must be present not everything.
#*******************************WARNING**********************************************
#Make sure these folders names are already created while using the 'move() / copy()'*
#************************************************************************************
filePath1 = 'C:\\Users\\manimani\\Downloads'        #Downloads folder
filePath2 = 'F:\\Projects\\Python\\Examples1'       #Downloads folder
filePath3 = 'C:\\Users\\manimani\\python'           #Python program files folder
filePath4 = 'F:\\Backup'                            #Backup folder
filePath5 = 'F:\\PDF_Downloads'                     #PDF files folder
filePath6 = 'C:\\Users\\manimani\\Videos'           #Videos folder
filePath7 = 'F:\\WordFile_Downloads'                #WordFiles folder
filePath8 = 'F:\\ExeFiles_Downloads'                #ExeFiles folder
filePath9 = 'F:\\Image_Downloads'                   #JPEG_Files folder

#To change the directory from where the files to look // ***Source Directory***
os.chdir(filePath8)

#To get the list of files in the specific source directory
myFiles = os.listdir()

#To move all the '.docx' files to a different location // ***Destination Directory***
for filename in myFiles:
    if filename.endswith('.docx'):
        print('Moving the file : ' + filename)
        shutil.move(filename, filePath4) #Destination directory name


print('All the files are moved as directed. Thanks')
manick
  • 11
  • 2
0
import os, shutil
src1 = "c:/users/mick/temp/"
src2 = "c:/users/mick/"
dist = "c:/users/mick/newfolder"
if not os.path.isdir(dist) : os.mkdir(dist)
for src in [src1, src2] :
    for f in os.listdir(src) :
        if f.endswith(".txt") :
            #print(src) # For testing purposes
            shutil.copy(src, dist)
            os.remove(src)

I think the error was that you were trying to copy a directory as a file, but you forgot to go through the files in the directory. You were just doing shutil.copy("c:/users/mick/temp/", c:/users/mick/newfolder/").

andrew
  • 448
  • 7
  • 13