Hướng dẫn copy files from multiple folders to one folder python - sao chép tệp từ nhiều thư mục vào một thư mục python

Cách dễ nhất để sao chép các tệp từ nhiều thư mục vào chỉ một thư mục sử dụng Python là gì? Để rõ ràng hơn, tôi có một cái cây trông như thế này

+Home_Directory
  ++folder1
   -csv1.csv
   -csv2.csv
  ++folder2
   -csv3.csv
   -csv4.csv

Và tôi muốn đặt CSV1, CSV2, ... vv tất cả vào một số thư mục được chỉ định mà không cần phân cấp thư mục.

+some_folder
   -csv1.csv
   -csv2.csv
   -csv3.csv
   -csv4.csv

Một số giải pháp tôi đã xem xét:

Sử dụng SOWN.CopyTree sẽ không hoạt động vì nó sẽ bảo tồn cấu trúc tệp không phải là thứ tôi muốn.

Mã tôi đang chơi rất giống với những gì được đăng trong câu hỏi này: Sao chép nhiều tệp trong Python, vấn đề là tôi không biết làm thế nào để thực hiện việc lặp lại này. Có lẽ nó sẽ chỉ là một vòng khác cho vòng lặp này nhưng tôi không đủ quen thuộc với các thư viện HĐH và SHOTIL để biết chính xác những gì tôi đang lặp lại. Bất kỳ sự giúp đỡ về điều này?

Hướng dẫn copy files from multiple folders to one folder python - sao chép tệp từ nhiều thư mục vào một thư mục python

Hỏi ngày 16 tháng 4 năm 2015 lúc 20:30Apr 16, 2015 at 20:30

1

Đây là những gì tôi nghĩ đến. Tôi giả sử bạn chỉ rút các tệp CSV từ 1 thư mục.

RootDir1 = r'*your directory*'
TargetFolder = r'*your target folder*'
for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
        for name in files:
            if name.endswith('.csv'):
                print "Found"
                SourceFolder = os.path.join(root,name)
                shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder

Chỉnh sửa: Thiếu A 'ở cuối rootDir1. Bạn cũng có thể sử dụng điều này như một hướng dẫn bắt đầu để làm cho nó hoạt động như mong muốn.

Đã trả lời ngày 16 tháng 4 năm 2015 lúc 20:39Apr 16, 2015 at 20:39

Lược LốcLampPost

80611 Huy hiệu bạc28 Huy hiệu đồng11 silver badges28 bronze badges

2

import glob
import shutil
#import os
dst_dir = "E:/images"
print ('Named explicitly:')
for name in glob.glob('E:/ms/*/*/*'):    
    if name.endswith(".jpg") or name.endswith(".pdf")  : 
        shutil.copy(name, dst_dir)
        print ('\t', name)

Đã trả lời ngày 18 tháng 7 năm 2020 lúc 17:08Jul 18, 2020 at 17:08

1

Bạn có thể sử dụng nó để di chuyển tất cả các thư mục con từ cùng một thư mục khác đến bất cứ nơi nào bạn muốn.

import shutil
import os
path=r'* Your Path*'
arr = os.listdir(path)
for i in range(len(arr)):
  source_dir=path+'/'+arr[i]
  target_dir = r'*Target path*'
    
  file_names = os.listdir(source_dir)
    
  for file_name in file_names:
      shutil.move(os.path.join(source_dir, file_name), target_dir)

Đã trả lời ngày 25 tháng 5 năm 2021 lúc 8:15May 25, 2021 at 8:15

Hướng dẫn copy files from multiple folders to one folder python - sao chép tệp từ nhiều thư mục vào một thư mục python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    ĐọcOS and Shutil module.

    Approach:

    1. Bàn luận
    2. Trong bài viết này, chúng tôi sẽ thảo luận về cách di chuyển nhiều thư mục vào một thư mục. Điều này có thể được thực hiện bằng cách sử dụng mô -đun HĐH Python và SHOTIL.
    3. Nhận thư mục hiện tại và danh sách các thư mục bạn muốn hợp nhất.
    4. Vòng lặp qua danh sách các thư mục và lưu trữ nội dung của họ trong một danh sách. Ở đây, chúng tôi đã lưu trữ chúng trong từ điển để chúng tôi có thể có tên của thư mục làm khóa và nội dung của nó dưới dạng danh sách giá trị.

    Chỉ định thư mục mà bạn muốn hợp nhất tất cả các thư mục khác. Nếu thư mục tồn tại thì chúng ta sẽ tốt nhưng nếu thư mục không tồn tại thì hãy tạo một thư mục mới.

    Vòng lặp qua từ điển và di chuyển tất cả nội dung của tất cả các thư mục được liệt kê bên trong thư mục hợp nhất.Below code does the following:

    • Hãy để thực hiện cách tiếp cận này từng bước:
    • Bước 1: Mã dưới đây làm như sau:
    • Nhận thư mục hiện tại.

    Python3

    Liệt kê tất cả các thư mục mà bạn muốn hợp nhất.

    Lưu trữ nội dung của tất cả các thư mục được liệt kê trong từ điển với tên thư mục là khóa và nội dung của nó dưới dạng danh sách giá trị.

    current_folder = os.getcwd() 

    ____10=

    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    2
    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    3
    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    4
    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    5
    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    4
    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    7
    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    8

    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    9=
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    1

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    2
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    3
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    4
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    5
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    6

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    7
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    8=
    import glob
    import shutil
    #import os
    dst_dir = "E:/images"
    print ('Named explicitly:')
    for name in glob.glob('E:/ms/*/*/*'):    
        if name.endswith(".jpg") or name.endswith(".pdf")  : 
            shutil.copy(name, dst_dir)
            print ('\t', name)
    
    0
    Creates the merge folder if it does not already exist.

    Python3

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    7
    import glob
    import shutil
    #import os
    dst_dir = "E:/images"
    print ('Named explicitly:')
    for name in glob.glob('E:/ms/*/*/*'):    
        if name.endswith(".jpg") or name.endswith(".pdf")  : 
            shutil.copy(name, dst_dir)
            print ('\t', name)
    
    2=
    import glob
    import shutil
    #import os
    dst_dir = "E:/images"
    print ('Named explicitly:')
    for name in glob.glob('E:/ms/*/*/*'):    
        if name.endswith(".jpg") or name.endswith(".pdf")  : 
            shutil.copy(name, dst_dir)
            print ('\t', name)
    
    4

    Bước 2: Tạo thư mục hợp nhất nếu nó chưa tồn tại.

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    7
    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    2
    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    3

    import glob
    import shutil
    #import os
    dst_dir = "E:/images"
    print ('Named explicitly:')
    for name in glob.glob('E:/ms/*/*/*'):    
        if name.endswith(".jpg") or name.endswith(".pdf")  : 
            shutil.copy(name, dst_dir)
            print ('\t', name)
    
    5
    import glob
    import shutil
    #import os
    dst_dir = "E:/images"
    print ('Named explicitly:')
    for name in glob.glob('E:/ms/*/*/*'):    
        if name.endswith(".jpg") or name.endswith(".pdf")  : 
            shutil.copy(name, dst_dir)
            print ('\t', name)
    
    6

    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    4
    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    9

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    7
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    8=
    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    0

    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    4
    Folder 1
        File 1
        File 2
    Folder 2
        File 3
        File 4
    Folder 3
        File 5
        File 6
    Folder 4
        File 7
        File 8
    merge_folder (Empty)
    move_script.py
    4
    Folder 1
        File 1
        File 2
    Folder 2
        File 3
        File 4
    Folder 3
        File 5
        File 6
    Folder 4
        File 7
        File 8
    merge_folder (Empty)
    move_script.py
    5

    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    4
    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    5=
    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    7

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    7
    Folder 1
        File 1
        File 2
    Folder 2
        File 3
        File 4
    Folder 3
        File 5
        File 6
    Folder 4
        File 7
        File 8
    merge_folder (Empty)
    move_script.py
    1
    Folder 1
        File 1
        File 2
    Folder 2
        File 3
        File 4
    Folder 3
        File 5
        File 6
    Folder 4
        File 7
        File 8
    merge_folder (Empty)
    move_script.py
    2

    Folder 1 (Empty)
    Folder 2 (Empty)
    Folder 3 (Empty)
    Folder 4 (Untouched)
        File 7
        File 8
    merge_folder
        File 1
        File 2
        File 3
        File 4
        File 5
        File 6
    move_script.py
    2

    Folder 1
        File 1
        File 2
    Folder 2
        File 3
        File 4
    Folder 3
        File 5
        File 6
    Folder 4
        File 7
        File 8
    merge_folder (Empty)
    move_script.py
    6=
    Folder 1
        File 1
        File 2
    Folder 2
        File 3
        File 4
    Folder 3
        File 5
        File 6
    Folder 4
        File 7
        File 8
    merge_folder (Empty)
    move_script.py
    8
    Below code does the following:

    • Folder 1
          File 1
          File 2
      Folder 2
          File 3
          File 4
      Folder 3
          File 5
          File 6
      Folder 4
          File 7
          File 8
      merge_folder (Empty)
      move_script.py
      9=
      Folder 1 (Empty)
      Folder 2 (Empty)
      Folder 3 (Empty)
      Folder 4 (Untouched)
          File 7
          File 8
      merge_folder
          File 1
          File 2
          File 3
          File 4
          File 5
          File 6
      move_script.py
      1
    • Bước 3: Mã dưới đây làm như sau:

    Python3

    Vòng lặp qua từ điển với tất cả các thư mục.

    Bây giờ lặp qua nội dung của từng thư mục và từng người một chuyển chúng đến thư mục hợp nhất.

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    2
    Folder 1 (Empty)
    Folder 2 (Empty)
    Folder 3 (Empty)
    Folder 4 (Untouched)
        File 7
        File 8
    merge_folder
        File 1
        File 2
        File 3
        File 4
        File 5
        File 6
    move_script.py
    4
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    4
    Folder 1 (Empty)
    Folder 2 (Empty)
    Folder 3 (Empty)
    Folder 4 (Untouched)
        File 7
        File 8
    merge_folder
        File 1
        File 2
        File 3
        File 4
        File 5
        File 6
    move_script.py
    6

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    7
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    2
    Folder 1 (Empty)
    Folder 2 (Empty)
    Folder 3 (Empty)
    Folder 4 (Untouched)
        File 7
        File 8
    merge_folder
        File 1
        File 2
        File 3
        File 4
        File 5
        File 6
    move_script.py
    9
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    4 current_folder 1

    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    4=5

    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    4current_folder 3=

    Python3

    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    4=1= =3

    Mã hoàn chỉnh:

    =6 =7

    =6 =9

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    7
    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    2
    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    3

    import glob
    import shutil
    #import os
    dst_dir = "E:/images"
    print ('Named explicitly:')
    for name in glob.glob('E:/ms/*/*/*'):    
        if name.endswith(".jpg") or name.endswith(".pdf")  : 
            shutil.copy(name, dst_dir)
            print ('\t', name)
    
    5
    import glob
    import shutil
    #import os
    dst_dir = "E:/images"
    print ('Named explicitly:')
    for name in glob.glob('E:/ms/*/*/*'):    
        if name.endswith(".jpg") or name.endswith(".pdf")  : 
            shutil.copy(name, dst_dir)
            print ('\t', name)
    
    6

    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    4
    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    9

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    7
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    8=
    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    0

    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    4
    Folder 1
        File 1
        File 2
    Folder 2
        File 3
        File 4
    Folder 3
        File 5
        File 6
    Folder 4
        File 7
        File 8
    merge_folder (Empty)
    move_script.py
    4
    Folder 1
        File 1
        File 2
    Folder 2
        File 3
        File 4
    Folder 3
        File 5
        File 6
    Folder 4
        File 7
        File 8
    merge_folder (Empty)
    move_script.py
    5

    Liệt kê tất cả các thư mục mà bạn muốn hợp nhất.

    Lưu trữ nội dung của tất cả các thư mục được liệt kê trong từ điển với tên thư mục là khóa và nội dung của nó dưới dạng danh sách giá trị.

    current_folder = os.getcwd() 

    ____10=

    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    2
    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    3
    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    4
    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    5
    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    4
    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    7
    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    8

    +some_folder
       -csv1.csv
       -csv2.csv
       -csv3.csv
       -csv4.csv
    
    9=
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    1

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    2
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    3
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    4
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    5
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    6

    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    4
    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    5=
    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    7

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    7
    Folder 1
        File 1
        File 2
    Folder 2
        File 3
        File 4
    Folder 3
        File 5
        File 6
    Folder 4
        File 7
        File 8
    merge_folder (Empty)
    move_script.py
    1
    Folder 1
        File 1
        File 2
    Folder 2
        File 3
        File 4
    Folder 3
        File 5
        File 6
    Folder 4
        File 7
        File 8
    merge_folder (Empty)
    move_script.py
    2

    Folder 1 (Empty)
    Folder 2 (Empty)
    Folder 3 (Empty)
    Folder 4 (Untouched)
        File 7
        File 8
    merge_folder
        File 1
        File 2
        File 3
        File 4
        File 5
        File 6
    move_script.py
    2

    Vòng lặp qua từ điển với tất cả các thư mục.

    Bây giờ lặp qua nội dung của từng thư mục và từng người một chuyển chúng đến thư mục hợp nhất.

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    2
    Folder 1 (Empty)
    Folder 2 (Empty)
    Folder 3 (Empty)
    Folder 4 (Untouched)
        File 7
        File 8
    merge_folder
        File 1
        File 2
        File 3
        File 4
        File 5
        File 6
    move_script.py
    4
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    4
    Folder 1 (Empty)
    Folder 2 (Empty)
    Folder 3 (Empty)
    Folder 4 (Untouched)
        File 7
        File 8
    merge_folder
        File 1
        File 2
        File 3
        File 4
        File 5
        File 6
    move_script.py
    6

    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    7
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    2
    Folder 1 (Empty)
    Folder 2 (Empty)
    Folder 3 (Empty)
    Folder 4 (Untouched)
        File 7
        File 8
    merge_folder
        File 1
        File 2
        File 3
        File 4
        File 5
        File 6
    move_script.py
    9
    RootDir1 = r'*your directory*'
    TargetFolder = r'*your target folder*'
    for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
            for name in files:
                if name.endswith('.csv'):
                    print "Found"
                    SourceFolder = os.path.join(root,name)
                    shutil.copy2(SourceFolder, TargetFolder) #copies csv to new folder
    
    4 current_folder 1

    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    4=5

    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    4current_folder 3=

    Folder 1
        File 1
        File 2
    Folder 2
        File 3
        File 4
    Folder 3
        File 5
        File 6
    Folder 4
        File 7
        File 8
    merge_folder (Empty)
    move_script.py

    import shutil
    import os
    path=r'* Your Path*'
    arr = os.listdir(path)
    for i in range(len(arr)):
      source_dir=path+'/'+arr[i]
      target_dir = r'*Target path*'
        
      file_names = os.listdir(source_dir)
        
      for file_name in file_names:
          shutil.move(os.path.join(source_dir, file_name), target_dir)
    
    4=1= =3

    Folder 1 (Empty)
    Folder 2 (Empty)
    Folder 3 (Empty)
    Folder 4 (Untouched)
        File 7
        File 8
    merge_folder
        File 1
        File 2
        File 3
        File 4
        File 5
        File 6
    move_script.py

    Mã hoàn chỉnh:

    Hướng dẫn copy files from multiple folders to one folder python - sao chép tệp từ nhiều thư mục vào một thư mục python


    Làm cách nào để sao chép một tệp từ nhiều thư mục vào một thư mục trong Python?

    Bạn có thể sao chép nội dung của một thư mục sang một thư mục khác bằng cách sử dụng các phương thức SOWN.Copy (), Sould.Copy2 () và Sould.CopyTree () của mô -đun này.shutil. copy(), shutil. copy2() and shutil. copytree() methods of this module.

    Làm cách nào để sao chép một tệp từ nhiều thư mục sang một thư mục trong Python?

    Phương thức SOWN.MOVE () di chuyển một cách đệ quy một tệp hoặc thư mục (nguồn) sang một vị trí khác (đích) và trả về đích. Nếu thư mục đích đã tồn tại thì SRC sẽ được di chuyển bên trong thư mục đó. Nếu đích đến đã tồn tại nhưng không phải là một thư mục thì nó có thể bị ghi đè tùy thuộc vào hệ điều hành. move() method Recursively moves a file or directory (source) to another location (destination) and returns the destination. If the destination directory already exists then src is moved inside that directory. If the destination already exists but is not a directory then it may be overwritten depending on os.

    Làm cách nào để sao chép một tệp từ nhiều thư mục sang một thư mục?

    Di chuyển hoặc sao chép tệp trong các thư mục con vào một thư mục..
    Mở một cửa sổ nhắc lệnh..
    Chạy các lệnh sau, từng cái một và nhấn enter sau mỗi dòng: md "d: \ all snaps" cd /d "d: \ kỳ nghỉ snaps \ 2016" cho /r %d in (*) do sao chép " %d""D: \ Tất cả snaps \".

    Làm cách nào để sao chép nhiều tệp từ thư mục này sang thư mục khác trong Python?

    Các bước dưới đây cho thấy cách sao chép một tệp từ thư mục này sang thư mục khác ...
    Tìm đường dẫn của một tập tin.Chúng ta có thể sao chép một tệp bằng cả đường dẫn tương đối và đường dẫn tuyệt đối.....
    Sử dụng hàm SOWL.Copy ().....
    Sử dụng hàm Os.ListDir () và SOWLIL COPY () để sao chép tất cả các tệp.....
    Sử dụng hàm copytree () để sao chép toàn bộ thư mục ..