【问题标题】:Copy files from multiple specific subfolders in Python从 Python 中的多个特定子文件夹复制文件
【发布时间】:2019-06-08 16:13:27
【问题描述】:

在文件路径 D:/src 下,我有图像文件夹及其子文件夹,它们的结构如下:

Folder A
- Subfolder a
- Subfolder b
- Subfolder c
Folder B
- Subfolder a
- Subfolder b
- Subfolder c
- Subfolder d
Folder C
- Subfolder a
- Subfolder b
- Subfolder c
...

我想将子文件夹 b 中的所有 .jpg 文件从文件夹 A、B、C 等复制到 D:/dst 中的新文件夹子文件夹 b。我怎样才能在 Python 中做到这一点?谢谢。

Subfolder b
-xxx.jpg
-xyx.jpg
-yxz.jpg
...

以下是我从以下链接中发现的内容,可能会有所帮助:

Copy certain files from one folder to another using python

import os;
import shutil;
import glob;

source="C:/Users/X/Pictures/test/Z.jpg"
dest="C:/Users/Public/Image"

    if os.path.exists(dest):
    print("this folder exit in this dir")
else:
    dir = os.mkdir(dest)

for file in glob._iglob(os.path.join(source),""):
    shutil.copy(file,dest)
    print("done")

【问题讨论】:

  • 你想复制到哪里?
  • 到 D:/dest/Subfolder b 下的新文件夹。我更新了问题。谢谢。
  • 感谢所有帮助。我会尝试这些方法并让你知道结果。 :)

标签: python operating-system glob shutil


【解决方案1】:

试试这个

import os
from os.path import join, isfile

BASE_PATH = 'd:/test'
SUBFOLDER = 'Subfolder b'

for folder, subfolders, *_ in os.walk(BASE_PATH):
    if SUBFOLDER in subfolders:
        full_path = join(BASE_PATH, folder, SUBFOLDER)
        files = [f for f in os.listdir(full_path) if isfile(join(full_path, f)) and f.lower().endswith(('.jpg', '.jpeg'))]
        for f in files:
            file_path = join(full_path, f)
            print (f'Copy {f} somewehere')

【讨论】:

    【解决方案2】:

    假设你有 2 层嵌套

    root_dir = './data'
    dest_dir = './new_location'
    os.listdir(root_dir)
    
    for folder in os.listdir(root_dir):
        folder_path = os.path.join(root_dir, folder)
        if os.path.isdir(folder_path):
            for subfolder in os.listdir(folder_path):
                if subfolder == 'Subfolder B':
                    subfolder_path = os.path.join(root_dir, folder, subfolder)
                    print(subfolder_path)
                    for filename in os.listdir(subfolder_path):
                        file_path = os.path.join(root_dir, folder, subfolder, filename)
                        dest_path = os.path.join(dest_dir, filename)
                        shutil.copy(file_path, dest_path)
                        print("Copied ", file_path, "to", dest_path)
    

    您只需要 2 个 for 循环,在内部 for 循环中您只需检查文件夹名称是否与 Subfolder B 匹配。如果是,则将该目录中的所有文件复制到您的目标文件夹。

    【讨论】:

      【解决方案3】:

      这是一个应该完成工作的简短脚本...

      import os
      
      # list all the directories in current directory
      dirs = [x[0] for x in os.walk("D:/src")]
      
      for d in dirs:
          ## list all files in A/b/*, B/b/*, C/b/*...
          files_to_copy = os.listdir(os.path.join(d, "b"))  
          for f in files_to_copy:
              if f.endswith(".jpg"):  ## copy the relevant files to dest
                  shutil.copy(os.path.join(d, "b", f), os.path.join(dest, f))
      

      【讨论】:

        【解决方案4】:
        import shutil
        import os
         
         
        def move_files(source_director, destination_director):
            # select the files in the source directory
            fisiere = [fisier for fisier in os.listdir(source_director) if os.path.isfile(os.path.join(source_director, fisier))]
            print(fisiere)
            # each file in the source directory is moved to the destination directory
            for fisier in fisiere:
                shutil.move(source_director + '\\' + fisier, destination_director)
         
        def copy_files(source_director, destination_director):
            # select the files in the source directory
            fisiere = [fisier for fisier in os.listdir(source_director) if os.path.isfile(os.path.join(source_director, fisier))]
            print(fisiere)
            # each file in the source directory is copied to the destination directory
            for fisier in fisiere:
                shutil.copy(source_director + '\\' + fisier, destination_director)
         
        if __name__ == '__main__':
             # WARNING: paths to directories must be written with '\\' (double backslash)
             # here you set the DIN directory you want to move / copy the files
        
            source_director = 'c:\\Folder'
         
            destination_director = 'c:\\Folder\\Subfolder1\\Subfolder2'
         
            #CASE 1
            # move_files(source_director, destination_director)
         
            #CASE 2
            copy_files(source_director, destination_director)
        

        此代码的来源:

        https://neculaifantanaru.com/en/python-copy-or-moves-files-from-a-folder-to-another-folder.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-17
          • 2021-04-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多