简体   繁体   English

Python shutil.move 创建无法打开的文件夹

[英]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.我正在尝试实现一个简单的脚本,将 .mp4 文件从一个位置移动到另一个位置。 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您可以使用两个内置包 os 或 shutil 之一来执行相同的操作

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.为了使shutl正常工作,我们需要将文件名附加到目标文件夹的绝对路径。

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 .例如,我的目标路径从C:\\Dive_Videos\\BROW\\2020-10-30C:\\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.这将文件从源路径( C:\\Dive_Videos\\session_97cc7372_2020-10-30_140624\\1_09196fa6_BROW_305B_fail\\test.mp4 )移动到目标路径。 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. destination_path = os.path.join(des_path,date,file_name)解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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