[英]Python shutil.move creating unopenable folders
我正在嘗試實現一個簡單的腳本,將 .mp4 文件從一個位置移動到另一個位置。 打印語句指定源路徑和目標路徑。 這些文件已從源路徑中刪除,我認為正在移動,但無法打開這些文件夾。 如果有人能調查一下,那就太好了。 附上一張圖片供參考。
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
您可以使用兩個內置包 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')
目標路徑指向目標文件夾。 為了使shutl
正常工作,我們需要將文件名附加到目標文件夾的絕對路徑。
例如,我的目標路徑從C:\\Dive_Videos\\BROW\\2020-10-30
為C:\\Dive_Videos\\BROW\\2020-10-30\\test.mp4
。 這將文件從源路徑( C:\\Dive_Videos\\session_97cc7372_2020-10-30_140624\\1_09196fa6_BROW_305B_fail\\test.mp4
)移動到目標路徑。 即使在較早的情況下,文件移動到目標文件夾但無法打開。
destination_path = os.path.join(des_path,date,file_name)
解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.