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

标签 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()

最佳答案

看起来您只是在移动文件,而没有对文件夹做任何事情。因此,如果您尝试移动

C:\Users\Jeremy\Downloads\anime\pokemon.avi

H:\Movies from download folder\anime\pokemon.avi

它将失败,因为 H:\ 上还没有 anime 目录。

在遍历 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...

关于Python:使用 shutil.move 或 os.rename 移动文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23244283/

相关文章:

python - 我们可以用纯Python urllib登录一个网站,修改一些表单值,然后提交吗? (没有 Selenium 浏览器自动化)

objective-c - 重命名连接的 Action

python - 无法重命名由 .sum() 函数创建的数据框的列名称

PHP 重命名() file_exists()

python - Python shutil 模块中的 shutil.move() 方法

python - 使用 anaconda 发行版进行 Web 开发可以吗?

python - 在 pandas read_sql 中可选择 SQLalchemy 的方法

python - 如何找出某个地址范围内有多少客户端?

Python - 将文件移动到 Ubuntu 中的根目录

python - 我可以选择要在shutil 中存档的文件吗?