簡體   English   中英

Python:使用shutil.move或os.rename移動文件夾

[英]Python: Using shutil.move or os.rename to move folders

我編寫了一個腳本將視頻文件從一個目錄移動到另一個目錄,它還將使用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...

因為這些是MS Windows路徑,所以使用正斜杠代替並將路徑聲明為字符串文字 ; 例如

dir_dst = r"H:/Movies from download folder/"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM