【问题标题】:using Python to copy .npz files from a folder and its subfolders, all to some other single folder使用 Python 将 .npz 文件从文件夹及其子文件夹复制到其他单个文件夹
【发布时间】:2021-05-17 22:56:41
【问题描述】:

以下代码将文件夹C:/Users/toTEST中的所有.npz文件复制到文件夹C:/Users/archive中p>

import glob
import shutil
dest_dir = "C:/Users/archive"
for file in glob.glob(r'C:/Users/toTEST/*.npz'):
    print(file)
    shutil.copy(file, dest_dir)

但是 toTEST 文件夹有几个子文件夹,我也想将他们的 .npz 文件移动到 archive 文件夹中。如何有效地实现这一目标?

【问题讨论】:

    标签: glob subdirectory shutil


    【解决方案1】:

    这似乎可以解决问题:

    import shutil
    import glob
    
    path = 'C:/Users/toTEST/**/'
    dest = 'C:/Users/archive/'
    pattern = '*.npz'
    
    selectfiles = glob.glob(path + pattern, recursive=True)
    
    for file in selectfiles:
        shutil.copy2(file, dest)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 2016-02-28
      • 2011-07-19
      相关资源
      最近更新 更多