簡體   English   中英

Python - 將文件從一個文件夾移動到另一個文件夾

[英]Python - move files from one folder to another

在下面尋找我的代碼的擴展,所以當不滿足條件時,文件將被移動到“ERROR”文件夾。 誰能幫忙? 謝謝:-)

source = '/Users/XY/Desktop/OUT'
destination = '/Users/XY/Desktop/IN'

allfiles = glob.glob(os.path.join(source, '*S*'), recursive=True)
print("Files to move", allfiles)

for file_path in allfiles:
    dst_path = os.path.join(destination, os.path.basename(file_path))
    shutil.move(file_path, dst_path)
    print(f"Moved {file_path} -> {dst_path}")

我試圖在谷歌上搜索但沒有運氣。

for file_path in allfiles:
    if some_condition:
        dst_path = os.path.join(destination, os.path.basename(file_path))
    else:
        dst_path = os.path.join(error_folder, os.path.basename(file_path))
    shutil.move(file_path, dst_path)
    print(f"Moved {file_path} -> {dst_path}")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM