Python:使用shutil.move或os.rename移动文件夹

use*_*858 5 python rename shutil

我编写了一个脚本将视频文件从一个目录移动到另一个目录,它还将使用os.walk搜索子目录.但是,如果脚本找到一个视频文件,它将只移动文件而不是包含文件夹.我添加了一个if语句来检查包含文件夹是否与原始搜索文件夹不同.

我找不到实际将文件夹和文件移动(或重命名?)到不同目录的代码.我已经阅读/观看了很多关于移动文件的内容,并且有很多相关信息,但我无法找到任何移动文件夹的内容.

我试过使用shutil.move和os.rename,我两次都得到一个错误.当我尝试搜索问题时,我得到了很多关于如何移动文件的结果,或者如何更改python的当前工作目录.

任何建议(甚至如何用谷歌搜索来准确描述如何找到关于这个主题的教程)将非常感激.这是我的第一个真实世界的python程序,我学到了很多,但最后一步是让我失望!

编辑:当试图使用os.rename(src_file, dst_file)我得到错误WindowsError: error 3 The system cannot find the path specified.

shutil.move(src_file, dst_file)我试图获得ioerror errno 2 no such file or directory "H:\\Moviesfrom download...\OneOfTheVideoFilesNotInParentFolder 即文件夹和文件需要移动.

谢谢.

ps就像我说这是我在代码学院之外的第一个脚本所以任何随机的建议也将不胜感激.

import os
import shutil
import time

movietypes = ('.3gp', '.wmv', '.asf', '.avi', '.flv', '.mov', '.mp4', '.ogm', '.mkv',
'. mpg', '.mpg', '.nsc', '.nsv', '.nut', '.a52', '.tta', '.wav', '.ram', '.asf',
'.wmv', '. ogg', '.mka', '.vid', '.lac', '.aac', '.dts', '.tac',
'.dts', '.mbv')

filewrite = open('H:\\Movies from download folder\\Logs\\logstest.txt', 'w')
dir_src = "C:\\Users\\Jeremy\\Downloads\\"
dir_dst = "H:\\Movies from download folder\\"

for root, dirs, files in os.walk(dir_src):
    for file in files:
        if file.endswith(movietypes) == True:
           filestr = str(file)
           locationoffoundfile = os.path.realpath(os.path.join(root,filestr))
           folderitwasin = locationoffoundfile.replace(dir_src,'')
           folderitwasin = folderitwasin.replace(filestr,'')
           pathofdir = os.path.realpath(root) + "\\"
           if pathofdir != dir_src:
                src_file = locationoffoundfile
                dst_file = dir_dst + folderitwasin + filestr
                os.rename(src_file, dst_file) #****This line is the line im having issues with***
                print src_file
                print dst_file
                filewrite.write(file + " " + "needs to have dir and file moved Moved!" + '\n')
           else:
                src_file = os.path.join(dir_src, file)
                dst_file = os.path.join(dir_dst, file)
                print src_file
                print dst_file
                shutil.move(src_file, dst_file)
                filewrite.write(os.path.dirname(file) + '\n')
                filewrite.write(file + " " + "needs to have file moved Moved!" + '\n')
filewrite.close()
Run Code Online (Sandbox Code Playgroud)

Kev*_*vin 3

看起来您只是移动文件,而没有对文件夹执行任何操作。所以如果你尝试移动

C:\Users\Jeremy\Downloads\anime\pokemon.avi
Run Code Online (Sandbox Code Playgroud)

H:\Movies from download folder\anime\pokemon.avi
Run Code Online (Sandbox Code Playgroud)

它会失败,因为还没有anime目录H:\

在迭代 之前files,请迭代dirs以确保该目录存在于您的目标位置,并在必要时创建它。

for root, dirs, files in os.walk(dir_src):
    for dir in dirs:
        dest_dir = os.path.join(dir_dst, dir)
        if not os.path.isdir(dest_dir):
            os.mkdir(dest_dir)
    for file in files:
    #rest of code goes here as usual...
Run Code Online (Sandbox Code Playgroud)