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
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,
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 Shutilshutil.copytree[src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
- 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 [
2 trong Windows hoặcshutil.copytree[src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
3 trong UNIX. ]shutil.copytree[src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
- 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- 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ụ:
5 là một đường dẫn tuyệt đối để khám phá các mẫu. txtshutil.copytree[src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
- 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
7shutil.copytree[src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
- 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
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àmshutil.copytree[src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
copied expense.txt copied profit.txt copied revenue.txt
8 - Sử dụng hàm copytree[] để sao chép toàn bộ thư mục
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ênimport 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 đíchimport shutil source_dir = r"E:\demos\files\reports" destination_dir = r"E:\demos\files\account" shutil.copytree[source_dir, destination_dir]
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
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 đã choshutil.copytree[src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
- 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
4 để sao chép tệp hiện tại vào đường dẫn thư mục đíchshutil.copytree[src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
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
- Phương pháp này sao chép đệ quy toàn bộ cây thư mục bắt nguồn từ
1 vào thư mục có tênimport 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 đíchimport shutil source_dir = r"E:\demos\files\reports" destination_dir = r"E:\demos\files\account" shutil.copytree[source_dir, destination_dir]
- _______12_______8 quyết định liệu có nên đưa ra một ngoại lệ trong trường hợp
2 hoặc bất kỳ thư mục mẹ bị thiếu nào đã tồn tại hay chưaimport shutil source_dir = r"E:\demos\files\reports" destination_dir = r"E:\demos\files\account" shutil.copytree[source_dir, destination_dir]
- Sử dụng tham số
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ệpshutil.copyfile[src, dst, *, follow_symlinks=True]
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______
6. Vị trí tệp nguồn [đường dẫn] có nội dung cần sao chépshutil.copyfile[src, dst, *, follow_symlinks=True]
- _______26_______7. Vị trí tệp đích [đường dẫn] nơi nội dung từ tệp nguồn sẽ được dán
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ệpshutil.copyfile[src, dst, *, follow_symlinks=True]
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ì
0 sẽ được nâng lênimport 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']
- Nếu nguồn và đích trỏ đến cùng một tệp thì
1 sẽ được nâng lênimport 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']
- 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.txt8 sẽ sao chép nội dung của tệp nguồn sang tệp đích
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ệpXem 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]
2import 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]
0Ghi 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êncopy[] so với copyfiles[]
Trong khi các phương thức
copied expense.txt copied profit.txt copied revenue.txt8 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úngNế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.txt8 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.txt8 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.txt9 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]
1Trong khi các phương pháp
copied expense.txt copied profit.txt copied revenue.txt8 và
copied expense.txt copied profit.txt copied revenue.txt9 đề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
1 để lấy thông tin dấu thời gian trong khi phương thứcshutil.copy[src, dst, *, follow_symlinks=True]
copied expense.txt copied profit.txt copied revenue.txt
8 lưu các chế độ cấp phép từ phương thứcDestination 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]
3Phươ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
6. Vị trí tệp nguồn [đường dẫn] có nội dung cần sao chépshutil.copyfile[src, dst, *, follow_symlinks=True]
- _______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]
5Sao 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
Sao chép tệp bằng phương pháp shutil.copy[src, dst, *, follow_symlinks=True]
9
shutil.copy[src, dst, *, follow_symlinks=True]
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
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]
03import 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ỳ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]
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 UNIXHã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]
7Trê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]
8Sao 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
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]
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
08 và có những hạn chế tương 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]
- 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
09import 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]
- Trên Windows, giá trị trả về là giá trị được shell hệ thống trả về sau khi chạy lệ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]
10. Lấy lệnh đối số duy nhất là lệnh shell bên dưới sẽ được thực thiHã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_______0Mô-đ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_______1Phươ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
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 baoimport 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_______4Sao 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
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]
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_______5import 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