Bản sao đùa giỡn của poton

Trong hướng dẫn Python này, bạn sẽ học cách sử dụng các hàm khác nhau có sẵn trong các mô-đun os, Shutil và subprocess để sao chép tệp và thư mục từ vị trí này sang vị trí khác

Show

Sau khi đọc bài viết này, bạn sẽ học được. -

  • Cách sao chép tệp trong Python bằng cách sử dụng các phương thức
    copied expense.txt
    copied profit.txt
    copied revenue.txt
    8,
    copied expense.txt
    copied profit.txt
    copied revenue.txt
    9,
    shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
    0,
    shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
    1 của mô-đun Shutil
  • Mô-đun hệ điều hành và quy trình con để sao chép các tệp bằng cách sử dụng lệnh shell của hệ điều hành bên dưới (
    shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
    2 trong Windows hoặc
    shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
    3 trong UNIX. )
  • Cách sao chép tất cả các tệp từ một thư mục
  • Sao chép toàn bộ thư mục theo cách đệ quy

Mục lục

Các bước để sao chép tệp trong Python

Python cung cấp hỗ trợ mạnh mẽ cho việc xử lý tệp. Chúng tôi có thể sao chép một và nhiều tệp bằng các phương pháp khác nhau và phương pháp được sử dụng phổ biến nhất là phương pháp

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
4. Các bước dưới đây cho biết cách sao chép tệp từ thư mục này sang thư mục khác

  1. 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. Đường dẫn là vị trí của tệp trên đĩa
    Một đường dẫn tuyệt đối chứa danh sách thư mục đầy đủ cần thiết để định vị tệp. Ví dụ:

    shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
    5 là một đường dẫn tuyệt đối để khám phá các mẫu. txt

  2. Bộ ria te nói đùa. chức năng sao chép ()

    Mô-đun Shutil cung cấp một số chức năng để thực hiện các thao tác cấp cao trên tệp và bộ sưu tập tệp. Hàm _______0_______8 trong mô-đun này được sử dụng để sao chép tệp từ thư mục này sang thư mục khác
    Đầu tiên, nhập mô-đun Shutil và Truyền đường dẫn tệp nguồn và đường dẫn thư mục đích đến hàm

    shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
    7

  3. sử dụng hệ điều hành. listdir() và Shutil copy() để sao chép tất cả các tập tin

    Giả sử bạn muốn sao chép tất cả các tệp từ thư mục này sang thư mục khác, sau đó sử dụng hàm

    shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
    8 để liệt kê tất cả các tệp của thư mục nguồn, sau đó lặp lại danh sách bằng vòng lặp for và sao chép từng tệp bằng hàm
    copied expense.txt
    copied profit.txt
    copied revenue.txt
    8

  4. Sử dụng hàm copytree() để sao chép toàn bộ thư mục

    import shutil
    
    source_dir = r"E:\demos\files\reports"
    destination_dir = r"E:\demos\files\account"
    shutil.copytree(source_dir, destination_dir)
    0 sao chép đệ quy toàn bộ cây thư mục bắt nguồn từ
    import shutil
    
    source_dir = r"E:\demos\files\reports"
    destination_dir = r"E:\demos\files\account"
    shutil.copytree(source_dir, destination_dir)
    1 vào thư mục có tên
    import shutil
    
    source_dir = r"E:\demos\files\reports"
    destination_dir = r"E:\demos\files\account"
    shutil.copytree(source_dir, destination_dir)
    2 và trả về thư mục đích

ví dụ. Sao chép một tệp

Trong ví dụ này, chúng tôi đang sao chép lợi nhuận. txt từ thư mục báo cáo vào thư mục tài khoản

import shutil

src_path = r"E:\demos\files\report\profit.txt"
dst_path = r"E:\demos\files\account\profit.txt"
shutil.copy(src_path, dst_path)
print('Copied')

Sao chép tất cả các tệp từ một thư mục

Đôi khi chúng tôi muốn sao chép tất cả các tệp từ thư mục này sang thư mục khác. Thực hiện theo các bước dưới đây để sao chép tất cả các tệp từ một thư mục

  • Lưu trữ đường dẫn thư mục nguồn và đích thành hai biến
  • Lấy danh sách tất cả các tệp có trong thư mục nguồn bằng hàm
    shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
    8. Nó trả về một danh sách chứa tên của các tệp và thư mục trong thư mục đã cho
  • Lặp lại danh sách bằng vòng lặp for để lấy tên tệp riêng lẻ
  • Trong mỗi lần lặp, nối tên tệp hiện tại với đường dẫn thư mục nguồn
  • Bây giờ sử dụng phương pháp
    shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
    4 để sao chép tệp hiện tại vào đường dẫn thư mục đích

thí dụ

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)

đầu ra

_______0_______

Sao chép toàn bộ thư mục

Đôi khi chúng ta cần sao chép toàn bộ thư mục, bao gồm tất cả các tệp và thư mục con chứa trong đó. Sử dụng phương pháp _______12_______5 của mô-đun Shutil để sao chép thư mục theo cách đệ quy

_______2_______
  • Phương pháp này sao chép đệ quy toàn bộ cây thư mục bắt nguồn từ
    import shutil
    
    source_dir = r"E:\demos\files\reports"
    destination_dir = r"E:\demos\files\account"
    shutil.copytree(source_dir, destination_dir)
    1 vào thư mục có tên
    import shutil
    
    source_dir = r"E:\demos\files\reports"
    destination_dir = r"E:\demos\files\account"
    shutil.copytree(source_dir, destination_dir)
    2 và trả về thư mục đích
  • _______12_______8 quyết định liệu có nên đưa ra một ngoại lệ trong trường hợp
    import shutil
    
    source_dir = r"E:\demos\files\reports"
    destination_dir = r"E:\demos\files\account"
    shutil.copytree(source_dir, destination_dir)
    2 hoặc bất kỳ thư mục mẹ bị thiếu nào đã tồn tại hay chưa
  • Sử dụng tham số
    shutil.copyfile(src, dst, *, follow_symlinks=True)
    0 để chuyển bất kỳ chức năng nào trong bốn chức năng mà mô-đun Shutil cung cấp để sao chép tệp

thí dụ. Hãy xem cách sao chép thư mục báo cáo và tất cả nội dung của nó vào thư mục tài khoản

import shutil

source_dir = r"E:\demos\files\reports"
destination_dir = r"E:\demos\files\account"
shutil.copytree(source_dir, destination_dir)

Mô-đun trò đùa sau đó Sao chép tệp

Mô-đun đóng cửa Python cung cấp nhiều chức năng cao cấp để thực hiện sao chép và xóa tệp

Các chức năng này cung cấp một cách sao chép tệp được tối ưu hóa và do đó tiết kiệm thời gian thực hiện tác vụ mở, đọc, ghi và đóng tệp không cần thiết khi không có yêu cầu xử lý trong tệp đó

Mô-đun Shutil cung cấp bốn chức năng sau để sao chép tệp

Chức năng Mô tả
shutil.copyfile(src, dst, *, follow_symlinks=True)
1Sao chép đối tượng tệp từ nguồn tới đích. (Cần mở cả đường dẫn nguồn và đích)
shutil.copyfile(src, dst, *, follow_symlinks=True)
2Sao chép nội dung của tệp này sang tệp khác. Siêu dữ liệu của tệp sẽ không được sao chép.
shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
4sao chép nội dung của tệp nguồn vào tệp đích cùng với siêu dữ liệu.
shutil.copyfile(src, dst, *, follow_symlinks=True)
4sao chép siêu dữ liệu bổ sung cụ thể là dấu thời gian của tệp nguồn vào đíchshutil phương pháp sao chép

Để quyết định sử dụng chức năng nào để sao chép tệp, chúng ta phải xem xét các điểm sau

  • Ví dụ: Nếu ứng dụng của bạn sử dụng tài nguyên được chia sẻ, bạn nên sao chép tệp ở chế độ chặn thay vì không đồng bộ
  • Tính di động của nền tảng. Nếu ứng dụng của bạn chạy trên một hệ điều hành khác, bạn phải viết mã sẽ chạy trên một hệ điều hành khác mà không gặp bất kỳ sự cố nào
  • Chức năng này sử dụng nhiều tài nguyên vì một số chức năng sao chép liên quan đến việc mở tệp, đọc nội dung, ghi và đóng tệp. vì vậy hãy chọn theo nhu cầu của bạn
  • Bạn có muốn sao chép siêu dữ liệu cùng với nội dung tệp không?

Phương pháp _______26_______2

Phương thức copyfile() được sử dụng để sao chép nội dung của tệp này sang tệp khác. Siêu dữ liệu của tệp sẽ không được sao chép

________26______
  • shutil.copyfile(src, dst, *, follow_symlinks=True)
    6. Vị trí tệp nguồn (đường dẫn) có nội dung cần sao chép
  • _______26_______7. Vị trí tệp đích (đường dẫn) nơi nội dung từ tệp nguồn sẽ được dán
  • shutil.copyfile(src, dst, *, follow_symlinks=True)
    8. Giá trị mặc định cho điều này là đúng. Nếu nó sai và vị trí tệp nguồn là một liên kết tượng trưng thì một liên kết tượng trưng mới sẽ được tạo thay vì sao chép từ tệp

ghi chú. Siêu dữ liệu của tệp sẽ không được sao chép khi sử dụng phương pháp này

thí dụ

________37______

Mã của chúng tôi đã sao chép 'lợi nhuận. txt' trong thư mục đích. Đây là danh sách các tệp có trong thư mục đích

  • lợi nhuận. txt
  • doanh thu. txt

Sử dụng hàm

shutil.copyfile(src, dst, *, follow_symlinks=True)
9 để liệt kê tất cả các tệp có trong thư mục đích để xác minh kết quả

Sau đây là những điểm quan trọng

  • Cả nguồn và đích phải là một tệp. Nếu đích đến là một thư mục thì
    import shutil
    
    src_folder = r"E:\demos\files\reports"
    dst_folder = r"E:\demos\files\account"
    
    # file names
    src_file = src_folder + "\profit.txt"
    dst_file = dst_folder + "\profit.txt"
    
    shutil.copyfile(src_file, dst_file)
    print('Copied')
    0 sẽ được nâng lên
  • Nếu nguồn và đích trỏ đến cùng một tệp thì
    import shutil
    
    src_folder = r"E:\demos\files\reports"
    dst_folder = r"E:\demos\files\account"
    
    # file names
    src_file = src_folder + "\profit.txt"
    dst_file = dst_folder + "\profit.txt"
    
    shutil.copyfile(src_file, dst_file)
    print('Copied')
    1 sẽ được nâng lên
  • Nếu đích tồn tại nhưng có tên khác với tệp nguồn thì nội dung của tệp đích sẽ bị ghi đè

Để tránh các lỗi trên, hãy luôn bọc mã của bạn trong khối thử-ngoại trừ. Tham khảo ví dụ sau

________41______

đầu ra

________42______

Phương pháp _______2_______4

Mô-đun

import shutil

src_folder = r"E:\demos\files\reports"
dst_folder = r"E:\demos\files\account"

# file names
src_file = src_folder + "\profit.txt"
dst_file = dst_folder + "\profit.txt"

shutil.copyfile(src_file, dst_file)
print('Copied')
3 có thêm một phương thức để sao chép tệp gọi là phương thức
copied expense.txt
copied profit.txt
copied revenue.txt
8 sẽ sao chép nội dung của tệp nguồn sang tệp đích

_______46_______

Tương tự như phương pháp

import shutil

src_folder = r"E:\demos\files\reports"
dst_folder = r"E:\demos\files\account"

# file names
src_file = src_folder + "\profit.txt"
dst_file = dst_folder + "\profit.txt"

shutil.copyfile(src_file, dst_file)
print('Copied')
5, phương pháp này sẽ đưa ra '
import shutil

src_folder = r"E:\demos\files\reports"
dst_folder = r"E:\demos\files\account"

# file names
src_file = src_folder + "\profit.txt"
dst_file = dst_folder + "\profit.txt"

shutil.copyfile(src_file, dst_file)
print('Copied')
1' nếu nguồn và đích trỏ đến cùng một tệp

Xem xét ví dụ sau khi chúng tôi cố gắng sao chép tệp 'profit. txt' từ nguồn

import shutil

source_dir = r"E:\demos\files\reports"
destination_dir = r"E:\demos\files\account"
shutil.copytree(source_dir, destination_dir)
1 đến thư mục đích
import shutil

source_dir = r"E:\demos\files\reports"
destination_dir = r"E:\demos\files\account"
shutil.copytree(source_dir, destination_dir)
2

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
0

Ghi chú. Nếu tệp nguồn không được tìm thấy ở vị trí đó thì phương thức copy() không thể được thực thi và một

import shutil

src_folder = r"E:\demos\files\reports"
dst_folder = r"E:\demos\files\account"

# file names
src_file = src_folder + "\profit.txt"
dst_file = dst_folder + "\profit.txt"

shutil.copyfile(src_file, dst_file)
print('Copied')
9 sẽ xuất hiện. Nếu thư mục đích không thể ghi thì ‘_
import os
import shutil
from shutil import SameFileError

src_folder = r"E:\demos\files\reports"
dst_folder = r"E:\demos\files\account"

# printing the contents of the destination folder
print("Destination folder before copying::", os.listdir(dst_folder))

# file names
src_file = src_folder + "\profit.txt"
dst_file = dst_folder + "\profit.txt"

try:
    # copy file
    shutil.copyfile(src_file, dst_file)
    # destination folder after copying
    print("Destination after copying", os.listdir(dst_folder))
except SameFileError:
    print("We are trying to copy the same File")
except IsADirectoryError:
    print("The destination is a directory")
0‘ sẽ được nâng lên

copy() so với copyfiles()

Trong khi các phương thức

copied expense.txt
copied profit.txt
copied revenue.txt
8 và
import shutil

src_folder = r"E:\demos\files\reports"
dst_folder = r"E:\demos\files\account"

# file names
src_file = src_folder + "\profit.txt"
dst_file = dst_folder + "\profit.txt"

shutil.copyfile(src_file, dst_file)
print('Copied')
5 trong mô-đun Shutil hoạt động giống nhau trong việc sao chép tệp từ vị trí này sang vị trí khác, có một số khác biệt đáng kể giữa chúng

Nếu đường dẫn đích là 'tệp' hoặc 'thư mục' thì phương thức

copied expense.txt
copied profit.txt
copied revenue.txt
8 sẽ tạo một tệp theo tên của tệp nguồn

  • Phương thức _______0_______8 lưu thông tin siêu dữ liệu bổ sung về các chế độ cấp phép từ tệp nguồn đến tệp đích. _______37_______5 không lưu bất kỳ thông tin siêu dữ liệu nào
  • Vì phương thức _______0_______8 thực hiện một cuộc gọi bổ sung để lưu các chế độ cấp phép;

Phương thức

copied expense.txt
copied profit.txt
copied revenue.txt
8 gọi
import shutil

src_folder = r"E:\demos\files\reports"
dst_folder = r"E:\demos\files\account"

# file names
src_file = src_folder + "\profit.txt"
dst_file = dst_folder + "\profit.txt"

shutil.copyfile(src_file, dst_file)
print('Copied')
5 và
Destination folder before copying:: ['expense.txt', 'revenue.txt']
Destination after copying ['expense.txt', 'profit.txt', 'revenue.txt']
0 bên trong triển khai nội bộ của nó. Nhưng
import os
import shutil
from shutil import SameFileError

src_folder = r"E:\demos\files\reports"
dst_folder = r"E:\demos\files\account"

# printing the contents of the destination folder
print("Destination folder before copying::", os.listdir(dst_folder))

# file names
src_file = src_folder + "\profit.txt"
dst_file = dst_folder + "\profit.txt"

try:
    # copy file
    shutil.copyfile(src_file, dst_file)
    # destination folder after copying
    print("Destination after copying", os.listdir(dst_folder))
except SameFileError:
    print("We are trying to copy the same File")
except IsADirectoryError:
    print("The destination is a directory")
7() chỉ gọi
shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
1 bên trong quá trình triển khai của nó

Phương pháp _______26_______4

Trong

import shutil

src_folder = r"E:\demos\files\reports"
dst_folder = r"E:\demos\files\account"

# file names
src_file = src_folder + "\profit.txt"
dst_file = dst_folder + "\profit.txt"

shutil.copyfile(src_file, dst_file)
print('Copied')
3 có thêm một phương pháp gọi là
copied expense.txt
copied profit.txt
copied revenue.txt
9 có thể được sử dụng để sao chép tệp từ vị trí này sang vị trí khác

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
1

Trong khi các phương pháp

copied expense.txt
copied profit.txt
copied revenue.txt
8 và
copied expense.txt
copied profit.txt
copied revenue.txt
9 đều sao chép nội dung của tệp cùng với siêu dữ liệu, có hai sự khác biệt đáng kể giữa hai phương pháp này

  • Phương pháp _______0_______9 có thể được sử dụng để sao chép siêu dữ liệu bổ sung cụ thể là dấu thời gian của nguồn đến đích. Đây là phần bổ sung cho các chế độ cấp phép mà phương thức
    copied expense.txt
    copied profit.txt
    copied revenue.txt
    8 lưu
  • Phương thức
    copied expense.txt
    copied profit.txt
    copied revenue.txt
    9 gọi phương thức
    shutil.copy(src, dst, *, follow_symlinks=True)
    1 để lấy thông tin dấu thời gian trong khi phương thức
    copied expense.txt
    copied profit.txt
    copied revenue.txt
    8 lưu các chế độ cấp phép từ phương thức
    Destination folder before copying:: ['expense.txt', 'revenue.txt']
    Destination after copying ['expense.txt', 'profit.txt', 'revenue.txt']
    0

Các kịch bản mà phương thức này ném ra 'SameFileError' tương tự như phương thức copy(). Chúng ta có thể xem cách sử dụng phương pháp này để sao chép tệp có tên 'Sample. txt' từ nguồn đến đích

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
2

đầu ra

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
3

Phương pháp _______26_______1

Phương thức _______2_______1 được sử dụng để sao chép đối tượng tệp từ vị trí nguồn đến vị trí đích

Ngoài vị trí nguồn và đích, chúng ta có thể chuyển kích thước bộ đệm, cho biết phần tệp cần được sao chép. Giá trị mặc định cho điều này là 16 KB

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
4
  • shutil.copyfile(src, dst, *, follow_symlinks=True)
    6. Vị trí tệp nguồn (đường dẫn) có nội dung cần sao chép
  • _______26_______7. Vị trí tệp đích (đường dẫn) nơi nội dung từ tệp nguồn sẽ được dán
  • _______46_______8. Kích thước bộ đệm biểu thị số byte được lưu trong bộ nhớ trong quá trình sao chép. Kích thước mặc định mà hệ thống sử dụng là 16KB

Hãy để chúng tôi xem một ví dụ để kiếm lợi nhuận. txt từ thư mục báo cáo vào thư mục tài khoản

thí dụ

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
5

Sao chép tệp bằng mô-đun hệ điều hành

Mô-đun hệ điều hành Python cung cấp các chức năng có thể tương tác trong các hệ điều hành khác nhau. Chúng tôi phải sử dụng các lệnh sao chép cơ bản dành riêng cho hệ điều hành cụ thể. Các phương pháp có thể di động trên các hệ điều hành khác nhau

Phương thức _______18_______00 mở một đường ống để thực thi một lệnh mà chúng ta truyền vào đối tượng tệp. Phương thức này mở một đối tượng tệp ở chế độ đọc hoặc ghi. Đọc là một chế độ mặc định

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
6
  • import os
    import shutil
    
    source_folder = r"E:\demos\files\reports\\"
    destination_folder = r"E:\demos\files\account\\"
    
    # fetch all files
    for file_name in os.listdir(source_folder):
        # construct full file path
        source = source_folder + file_name
        destination = destination_folder + file_name
        # copy only files
        if os.path.isfile(source):
            shutil.copy(source, destination)
            print('copied', file_name)
    01. Lệnh sẽ được thực thi trong đối tượng tệp đang mở
  • import os
    import shutil
    
    source_folder = r"E:\demos\files\reports\\"
    destination_folder = r"E:\demos\files\account\\"
    
    # fetch all files
    for file_name in os.listdir(source_folder):
        # construct full file path
        source = source_folder + file_name
        destination = destination_folder + file_name
        # copy only files
        if os.path.isfile(source):
            shutil.copy(source, destination)
            print('copied', file_name)
    02. Chế độ có thể mở tệp và chế độ mặc định là
    import os
    import shutil
    
    source_folder = r"E:\demos\files\reports\\"
    destination_folder = r"E:\demos\files\account\\"
    
    # fetch all files
    for file_name in os.listdir(source_folder):
        # construct full file path
        source = source_folder + file_name
        destination = destination_folder + file_name
        # copy only files
        if os.path.isfile(source):
            shutil.copy(source, destination)
            print('copied', file_name)
    03
  • import os
    import shutil
    
    source_folder = r"E:\demos\files\reports\\"
    destination_folder = r"E:\demos\files\account\\"
    
    # fetch all files
    for file_name in os.listdir(source_folder):
        # construct full file path
        source = source_folder + file_name
        destination = destination_folder + file_name
        # copy only files
        if os.path.isfile(source):
            shutil.copy(source, destination)
            print('copied', file_name)
    04. Số lượng byte sẽ được lưu trữ trong bộ đệm. Nếu được đề cập là 0 thì nó sẽ không lưu bất kỳ

Lệnh shell cơ bản để sao chép là

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
2 đối với windows và
shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
3 đối với môi trường UNIX

Hãy cho chúng tôi xem cách sử dụng phương pháp này để sao chép một tệp có tên là lợi nhuận. txt từ thư mục nguồn sang thư mục đích

Trên Unix

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
7

Trên Windows

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
8

Sao chép tệp bằng phương pháp import os import shutil source_folder = r"E:\demos\files\reports\\" destination_folder = r"E:\demos\files\account\\" # fetch all files for file_name in os.listdir(source_folder): # construct full file path source = source_folder + file_name destination = destination_folder + file_name # copy only files if os.path.isfile(source): shutil.copy(source, destination) print('copied', file_name)07

Phương thức system() một lần nữa được sử dụng để thực thi tập lệnh trong shell hoặc lệnh OS. Giá trị trả về là trạng thái thoát của lệnh

  • Điều này được thực hiện bằng cách gọi chức năng C tiêu chuẩn
    import os
    import shutil
    
    source_folder = r"E:\demos\files\reports\\"
    destination_folder = r"E:\demos\files\account\\"
    
    # fetch all files
    for file_name in os.listdir(source_folder):
        # construct full file path
        source = source_folder + file_name
        destination = destination_folder + file_name
        # copy only files
        if os.path.isfile(source):
            shutil.copy(source, destination)
            print('copied', file_name)
    08 và có những hạn chế tương tự
  • Trên Unix, giá trị trả về là trạng thái thoát của quy trình được mã hóa theo định dạng được chỉ định cho
    import os
    import shutil
    
    source_folder = r"E:\demos\files\reports\\"
    destination_folder = r"E:\demos\files\account\\"
    
    # fetch all files
    for file_name in os.listdir(source_folder):
        # construct full file path
        source = source_folder + file_name
        destination = destination_folder + file_name
        # copy only files
        if os.path.isfile(source):
            shutil.copy(source, destination)
            print('copied', file_name)
    09
  • Trên Windows, giá trị trả về là giá trị được shell hệ thống trả về sau khi chạy lệnh.  
________18______ tôi

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
10. Lấy lệnh đối số duy nhất là lệnh shell bên dưới sẽ được thực thi

Hãy cho chúng tôi xem cách sử dụng phương pháp này để sao chép một tệp có tên là mẫu. txt từ thư mục nguồn sang thư mục đích

_______0_______0

Mô-đun quy trình con để sao chép tệp

Mô-đun quy trình con được sử dụng để thực thi/khởi chạy một quy trình con mới từ ứng dụng của chúng tôi, kết nối với các đường ống đầu vào/đầu ra/lỗi của chúng và lấy mã trả về của chúng. Mô-đun này dự định thay thế một số mô-đun và chức năng cũ hơn

_______0_______1

Phương pháp _______18_______11

Phương thức này có thể được sử dụng để thực thi bất kỳ lệnh nào được truyền dưới dạng đối số cho phương thức này và giá trị trả về sẽ là trạng thái kết thúc của lệnh đã được thực thi

_______0_______2
  • import os
    import shutil
    
    source_folder = r"E:\demos\files\reports\\"
    destination_folder = r"E:\demos\files\account\\"
    
    # fetch all files
    for file_name in os.listdir(source_folder):
        # construct full file path
        source = source_folder + file_name
        destination = destination_folder + file_name
        # copy only files
        if os.path.isfile(source):
            shutil.copy(source, destination)
            print('copied', file_name)
    12. Cần thiết cho tất cả các cuộc gọi và phải là một chuỗi hoặc một chuỗi các đối số chương trình. Phương thức call() thực sự chạy lệnh được đề cập bởi các đối số
  • import os
    import shutil
    
    source_folder = r"E:\demos\files\reports\\"
    destination_folder = r"E:\demos\files\account\\"
    
    # fetch all files
    for file_name in os.listdir(source_folder):
        # construct full file path
        source = source_folder + file_name
        destination = destination_folder + file_name
        # copy only files
        if os.path.isfile(source):
            shutil.copy(source, destination)
            print('copied', file_name)
    13, _
    import os
    import shutil
    
    source_folder = r"E:\demos\files\reports\\"
    destination_folder = r"E:\demos\files\account\\"
    
    # fetch all files
    for file_name in os.listdir(source_folder):
        # construct full file path
        source = source_folder + file_name
        destination = destination_folder + file_name
        # copy only files
        if os.path.isfile(source):
            shutil.copy(source, destination)
            print('copied', file_name)
    14 và
    import os
    import shutil
    
    source_folder = r"E:\demos\files\reports\\"
    destination_folder = r"E:\demos\files\account\\"
    
    # fetch all files
    for file_name in os.listdir(source_folder):
        # construct full file path
        source = source_folder + file_name
        destination = destination_folder + file_name
        # copy only files
        if os.path.isfile(source):
            shutil.copy(source, destination)
            print('copied', file_name)
    15 lần lượt chỉ định đầu vào tiêu chuẩn, đầu ra tiêu chuẩn và lỗi tiêu chuẩn của chương trình được thực hiện.  
  • import os
    import shutil
    
    source_folder = r"E:\demos\files\reports\\"
    destination_folder = r"E:\demos\files\account\\"
    
    # fetch all files
    for file_name in os.listdir(source_folder):
        # construct full file path
        source = source_folder + file_name
        destination = destination_folder + file_name
        # copy only files
        if os.path.isfile(source):
            shutil.copy(source, destination)
            print('copied', file_name)
    16. Nếu được đặt thành true, lệnh đã chỉ định sẽ được thực thi thông qua trình bao
_______0_______3

đầu ra

_______0_______4

Sao chép tệp bằng phương pháp import os import shutil source_folder = r"E:\demos\files\reports\\" destination_folder = r"E:\demos\files\account\\" # fetch all files for file_name in os.listdir(source_folder): # construct full file path source = source_folder + file_name destination = destination_folder + file_name # copy only files if os.path.isfile(source): shutil.copy(source, destination) print('copied', file_name)17

Phương thức _______18_______18 một lần nữa được sử dụng để thực thi các lệnh bên ngoài và nắm bắt đầu ra của nó. Điều này rất giống với phương pháp

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
19 với một tập hợp các đối số tương tự

_______0_______5

import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
20. Nếu giá trị này là _
import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
21 thì tất cả các kết thúc dòng sẽ được chuyển đổi thành
import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
22 như được mô tả đối với đối số chế độ xuống dòng phổ quát
import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
23 thành _
import os
import shutil

source_folder = r"E:\demos\files\reports\\"
destination_folder = r"E:\demos\files\account\\"

# fetch all files
for file_name in os.listdir(source_folder):
    # construct full file path
    source = source_folder + file_name
    destination = destination_folder + file_name
    # copy only files
    if os.path.isfile(source):
        shutil.copy(source, destination)
        print('copied', file_name)
24

Sự khác biệt giữa các hàm Shutil copy() và Shutil copy2() là gì?

Trò đùa là. phương thức copy2() giống hệt với Shutil. copy() ngoại trừ việc copy2() cũng cố gắng bảo toàn siêu dữ liệu của tệp. copy2() attempts to preserve file metadata as well.

Sự khác biệt giữa Copyfile và sao chép trong Python đóng cửa là gì?

copy() vs copyfile() - . copy() sẽ sao chép một tệp nếu đích là một thư mục trong khi copyfile() sẽ không thành công với lỗi 13. The copy() also sets the permission bits while copying the contents whereas the copyfile() only copies the data. The copy() will copy a file if the destination is a directory whereas the copyfile() will fail with error 13.

Sự khác biệt giữa bản sao và bản sao2 là gì?

bản sao() so với bản sao2() . Phương thức copy() gọi nội bộ copyfile() và copymode() trong khi copy2() thay thế lệnh gọi copymode() bằng copystat(). The copy() only sets permission bits whereas copy2() also updates the file metadata with timestamps. The copy() method calls copyfile() and copymode() internally whereas copy2() replaces the call to copymode() with copystat().

Cách tốt nhất để sao chép tệp trong Python là gì?

Các bước sao chép tệp bằng Python .
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. .
Bộ ria te nói đùa. chức năng sao chép (). .
sử dụng hệ điều hành. listdir() và Shutil copy() để sao chép tất cả các tập tin. .
Sử dụng hàm copytree() để sao chép toàn bộ thư mục