python之批次移動檔案

lurenj發表於2024-09-20
# coding: utf-8
import os
import shutil
import re

if __name__ == "__main__":
    src = r"C:\Users\Windows11\Desktop\test"  # 原資料夾路徑
    des = r"C:\Users\Windows11\Desktop\result"  # 目標資料夾路徑
    # 檔案篩選條件

    vpatch = input("請輸入虛擬路徑:")
    vpatch = re.split('[,;]', vpatch)
    for file in vpatch:
        # 遍歷原資料夾中的檔案
        mark = file + '.txt'
        full_file_name = os.path.join(src, mark)  # 把檔案的完整路徑得到
        print("要被移動的全檔案路徑全名:", full_file_name)
        if os.path.isfile(full_file_name):  # 用於判斷某一物件(需提供絕對路徑)是否為檔案
            new_file_name = shutil.move(full_file_name, des)  # shutil.copy函式放入原檔案的路徑檔案全名  然後放入目標資料夾
            print("移動後的全檔案路徑全名:", new_file_name)


相關文章