python小工具_(dá)某路徑全部子文件轉(zhuǎn)到某文件夾

# -*- coding: utf-8 -*-
# 移動(dòng)某文件下所有子文件夾里的文件,到指定文件夾
import os
import shutil

def move_files_to_all_folder( base_path):
    # 創(chuàng)建一個(gè)名為 "All" 的文件夾(如果它不存在)
    all_folder_path = os. path. join( base_path, "All")
    if not os. path. exists( all_folder_path):
        os. makedirs( all_folder_path)
   
    # 遍歷 base_path 目錄下的所有子文件夾
    for root, dirs, files in os. walk( base_path):
        # 跳過(guò) "All" 文件夾,防止將文件移動(dòng)到自己里面
        if root == all_folder_path:
            continue
       
        # 遍歷文件并將其移動(dòng)到 "All" 文件夾
        for file in files:
            file_path = os. path. join( root, file)
            shutil. move( file_path, all_folder_path)
            print( f 'Moved: { file_path } to { all_folder_path } ')

    print( f 'All files have been moved to { all_folder_path } ')

# 使用函數(shù)
base_directory = r "F: \J PG"   # 在這里輸入你要操作的文件夾路徑
move_files_to_all_folder( base_directory)
登錄后免費(fèi)查看全文
立即登錄
App下載
技術(shù)鄰APP
工程師必備
  • 項(xiàng)目客服
  • 培訓(xùn)客服
  • 平臺(tái)客服

TOP