如何在使用 os.walk [关闭] 后复制文件

问题描述 投票:0回答:0

我想将 PD 备份的许多不同文件夹中的某些文件复制到我的本地 PC。这些特定文件将以“p”开头并以“long0.txt”结尾。运行 os.walk 并进行更改后,我可以复制所需的文件。现在的问题是如何将文件路径附加到最终文件名?

import os
import shutil
import glob
import pathlib
# ---------------------------------------------------------------------------
source_path = r"\\192.168.1.47\Operation\PD Backup\\"
dest_path = r"C:\Users\hudin\Documents\Test2"
for root, dirs, filename in os.walk(source_path, topdown=True):
      for file in filename:
          if file.startswith('p') and file.endswith('long0.txt'):
                # print(file)
                if os.path.isfile(os.path.join(root, file)):
                    file1 = os.path.join(os.getcwd()+file)
                    # shutil.copy2(os.path.join(root, file), os.path.join(dest_path, file))
                    shutil.copy2(os.path.join(root, file), os.path.join(dest_path, file1))
python shutil
© www.soinside.com 2019 - 2024. All rights reserved.