简体   繁体   中英

Python shutil.move creating unopenable folders

I am trying to implement a simple script where I move a .mp4 file from one location to another. The print statements specify the source and destination path. The files are removed from the source path and I think are getting moved but these folders cannot be opened. It would be great if someone can look into this. Attached a picture for reference.

def moving_folders(divers,path_from):
    
    session_list = os.listdir(path_from)
#     print(session_list)
    for session in session_list:
        name_split = session.split('_')
        if name_split[0] == 'session':
            
            session_name = '_'.join(name_split)
            date = name_split[2]
            print(date,session_name)
            temp_path = os.path.join(path_from,session_name)
            files_in_path = os.listdir(temp_path)
            print("files_in_path",files_in_path)
            for file in files_in_path:
                if '.' not in file:
                    mp4_path = os.path.join(temp_path,file)
                    mp4_files = os.listdir(mp4_path)
                    for mp4 in mp4_files:
                        if '.mp4' in mp4:
                            src_path = os.path.join(mp4_path,mp4)
                            des_path = os.path.join(path_from,file.split("_")[2])
                            date_name = os.path.join(des_path,date)
                            print(src_path, "src_path")
                            print(dest_path, "date_name")
                            print("----[![enter image description here][1]][1]--------------")
                            os.makedirs(os.path.dirname(date_name), exist_ok=True)
                            shutil.move(src_path,date_name)```


  Source Path  C:\Dive_Videos\session_97cc7372_2020-10-30_140624\1_09196fa6_BROW_305B_fail\09196fa6_BROW_305B_fail_2.mp4 src_path
   Destination Path C:\Dive_Videos\BROW\2020-10-30 date_name


  [1]: https://i.stack.imgur.com/33lOU.png

You can use one of the two builtin packages os or shutil to do the same

import os
import shutil

# Move a file by renaming it's path
os.rename(r'E:\file.mp4', r'E:\new\file.mp4')

# Move a file from the directory d1 to d2
shutil.move(r'E:\file.mp4', r'E:\new\file.mp4')

The destination path was pointing to the destination folder. For shutl to work we need to append the file name to the absolute path of the destination folder.

For example, my destination path was updated from C:\\Dive_Videos\\BROW\\2020-10-30 to C:\\Dive_Videos\\BROW\\2020-10-30\\test.mp4 . This moved the file from the source path ( C:\\Dive_Videos\\session_97cc7372_2020-10-30_140624\\1_09196fa6_BROW_305B_fail\\test.mp4 )to the destination path. Even in the earlier case the files moved to destination folder but couldn't be opened.

destination_path = os.path.join(des_path,date,file_name) fixed the issue in my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM