2010-08-03 49 views
51

Cách sao chép tất cả các tệp trong một thư mục sang thư mục khác trong python. Tôi có đường dẫn nguồn và đường dẫn đích trong một chuỗi.sao chép nhiều tệp trong python

+0

[Làm thế nào để sao chép toàn bộ thư mục của tập tin vào một thư mục hiện có sử dụng Python?] (Http://stackoverflow.com/questions/1868714/how-do-i-copy -an-whole-directory-of-files-in-an-existing-directory-using-pyth) –

Trả lời

69

Bạn có thể sử dụng os.listdir() để có được các tập tin trong thư mục nguồn, os.path.isfile() để xem họ là các tập tin thường xuyên (bao gồm liên kết tượng trưng trên hệ thống * nix), và shutil.copy để làm việc sao chép.

Đoạn mã sau chỉ sao chép các tệp thông thường từ thư mục nguồn vào thư mục đích (tôi giả sử bạn không muốn bất kỳ thư mục con nào được sao chép).

import os 
import shutil 
src_files = os.listdir(src) 
for file_name in src_files: 
    full_file_name = os.path.join(src, file_name) 
    if (os.path.isfile(full_file_name)): 
     shutil.copy(full_file_name, dest) 
7

Nhìn vào shutil in the Python docs, cụ thể là lệnh copytree.

+0

Nhận xét tốt, nhưng nó có thể không phải là một tùy chọn nếu thư mục đã tồn tại vì một lý do nào đó như trong trường hợp của tôi. – Sven

12

Nếu bạn không muốn sao chép toàn bộ cây (với subdirs vv), hãy sử dụng hoặc glob.glob("path/to/dir/*.*") để nhận danh sách tất cả tên tệp, lặp lại danh sách và sử dụng shutil.copy để sao chép từng tệp.

for filename in glob.glob(os.path.join(source_dir, '*.*')): 
    shutil.copy(filename, dest_dir) 
+1

Lưu ý: Bạn có thể phải kiểm tra kết quả glob với os.path.isfile() để chắc chắn chúng là tên tập tin. Xem thêm câu trả lời của GreenMatt. Trong khi glob chỉ trả về tên tệp như os.listdir, nó vẫn trả về tên thư mục. Mẫu '*. *' Có thể là đủ, miễn là bạn không có tên tệp mở rộng hoặc dấu chấm trong tên thư mục. – Steven

0
import os 
import shutil 
os.chdir('C:\\') #Make sure you add your source and destination path below 

dir_src = ("C:\\foooo\\") 
dir_dst = ("C:\\toooo\\") 

for filename in os.listdir(dir_src): 
    if filename.endswith('.txt'): 
     shutil.copy(dir_src + filename, dir_dst) 
    print(filename) 
1
def recursive_copy_files(source_path, destination_path, override=False): 
""" 
Recursive copies files from source to destination directory. 
:param source_path: source directory 
:param destination_path: destination directory 
:param override if True all files will be overridden otherwise skip if file exist 
:return: count of copied files 
""" 
files_count = 0 
if not os.path.exists(destination_path): 
    os.mkdir(destination_path) 
items = glob.glob(source_path + '/*') 
for item in items: 
    if os.path.isdir(item): 
     path = os.path.join(destination_path, item.split('/')[-1]) 
     files_count += recursive_copy_files(source_path=item, destination_path=path, override=override) 
    else: 
     file = os.path.join(destination_path, item.split('/')[-1]) 
     if not os.path.exists(file) or override: 
      shutil.copyfile(item, file) 
      files_count += 1 
return files_count 
+1

Có thể giúp giải thích bằng lời cho mã của bạn –

+0

Tôi nghĩ bạn có ý nghĩa ** ghi đè **, chứ không phải ** ghi đè ** –

Các vấn đề liên quan