如何在python中使用shutil复制文件

时间:2014-09-09 17:20:02

标签: python file move shutil

我试图将.txt文件从一个dest复制到另一个。此代码正在运行但文件未复制。我做错了什么?

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)

4 个答案:

答案 0 :(得分:2)

您的for循环实际上并未搜索每个来源的文件。此外,您的for循环不是循环遍历每个源,而是srcsrc2中存在的字母。这种调整应该能够满足您的需求。

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)

你不应该为这种情况需要shutil,因为它只是一个更强大的os.rename,试图更好地处理不同的场景(据我所知)。但是,如果“newfolder1”尚未存在,则您希望将os.rename()替换为os.renames(),因为这会尝试在其间创建目录。

答案 1 :(得分:1)

shutils对复制非常有用,但是要移动文件

os.rename(oldpath, newpath)

答案 2 :(得分:1)

我知道这个问题有点老了。但这是实现任务的简单方法。您可以根据需要更改文件扩展名。对于复制,您可以使用copy()&移动哟可能会改变它移动()。希望这会有所帮助。

#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')

答案 3 :(得分: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)

我认为错误是您尝试将目录复制为文件,但是您忘记浏览目录中的文件。你刚刚做shutil.copy("c:/users/mick/temp/", c:/users/mick/newfolder/")