Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Using shutil.move or os.rename to move folders

I have written a script to move video files from one directory to another, it will also search sub directories using os.walk. however if the script finds a video file it will only move the file and not the containing folder. i have added an if statement to check if the containing folder is different to the original search folder.

i cant find the code to actually move(or rename?) the folder and file to a different directory. i have read/watch a lot on moving files and there is a lot of information on that, but i cant find anything for moving folders.

i have tried using shutil.move and os.rename and i get an error both times. when i try and search for the problem i get a lot of results about how to move files, or how to change the current working directory of python.

any advice(even how to phrase the google search to accuratly describe how to find a tutorial on the subject) would be really appreciated. it's my first real world python program and ive learnt a lot but this last step is wearing me down!

EDIT: when trying to use os.rename(src_file, dst_file) i get the error WindowsError: error 3 The system cannot find the path specified.

when trying shutil.move(src_file, dst_file) i get ioerror errno 2 no such file or directory "H:\\Moviesfrom download...\OneOfTheVideoFilesNotInParentFolder ie the folder and the file needs to move.

thanks.

ps like i said it's my first script outside of code academy so any random suggestions would also be appreciated.

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()
like image 583
user3560858 Avatar asked Oct 20 '22 09:10

user3560858


1 Answers

Looks like you're only moving files, without doing anything about the folders. So if you try to move

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

to

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

it will fail because there's no anime directory on H:\ yet.

Before iterating through files, iterate through dirs to ensure that the directory exists at your destination, creating it if necessary.

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...
like image 182
Kevin Avatar answered Oct 23 '22 04:10

Kevin