Trong hướng dẫn ngắn này, bạn sẽ thấy cách sao chép một tệp, từ thư mục này sang thư mục khác, sử dụng Python.
Để bắt đầu, đây là một mẫu mà bạn có thể sử dụng để sao chép một tệp trong Python bằng SOWLIL.CopyFile:
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]
Bây giờ, hãy xem các bước để áp dụng mẫu trên trong thực tế.
Bước 1: Chụp đường dẫn ban đầu
Để bắt đầu, hãy chụp đường dẫn nơi tệp của bạn hiện đang được lưu trữ.
Ví dụ: hãy giả sử rằng một tệp CSV được lưu trữ trong một thư mục có tên là Test_1:Test_1:
C: \ Users \ Ron \ Desktop \ test_1 \ Products.csv
Trong đó tên tệp CSV là ‘Sản phẩm‘ và phần mở rộng tệp là CSV.products‘ and the file extension is csv.
Bước 2: Chụp đường đích
Tiếp theo, chụp đường dẫn đích nơi bạn muốn sao chép tệp.
Ví dụ của chúng tôi, tệp sẽ được sao chép vào một thư mục có tên Test_2:Test_2:
C: \ Users \ Ron \ Desktop \ test_2 \ Products.csv
Bước 3: Sao chép tệp trong Python bằng cách sử dụng SOWL.CopyFile
Đối với bước cuối cùng, hãy sử dụng mẫu sau để sao chép tệp của bạn:
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]
Đảm bảo đặt ký tự ‘R‘ trước các đường dẫn của bạn để tránh lỗi sau:r‘ character before your paths to avoid the following error:
Cú pháp
Trong bối cảnh ví dụ của chúng tôi, mã hoàn chỉnh sẽ trông như thế này:
import shutil original = r'C:\Users\Ron\Desktop\Test_1\products.csv' target = r'C:\Users\Ron\Desktop\Test_2\products.csv' shutil.copyfile[original, target]
Nếu bạn chạy mã trong Python [được điều chỉnh theo đường dẫn của bạn], bạn sẽ thấy rằng tệp CSV của sản phẩm sẽ được sao chép vào thư mục Test_2.products‘ CSV file would be copied into the Test_2 folder.
Ngoài ra, bạn có thể sao chép một tệp có tên mới.
Chẳng hạn, hãy để sao chép tệp CSV gốc [với tên tệp của ‘Sản phẩm] vào vị trí mới với tên tệp mới [‘ New_Products ‘]:products‘] to the new location with a new file name [‘new_products‘]:
import shutil original = r'C:\Users\Ron\Desktop\Test_1\products.csv' target = r'C:\Users\Ron\Desktop\Test_2\new_products.csv' shutil.copyfile[original, target]
Tên tệp mới [được gọi là ‘new_products] sau đó sẽ được sao chép ở vị trí đích [thư mục test_2].new_products‘] would then be copied in the target location [the Test_2 folder].
Các nguyên tắc tương tự sẽ áp dụng cho các loại tệp khác. Chẳng hạn, hãy để giả sử rằng một tệp JPG có tên ‘Hình ảnh‘ được lưu trữ trong thư mục Test_1.image‘ is stored in the Test_1 folder.
Mã sau đây có thể được sử dụng để sao chép hình ảnh vào thư mục Test_2:Test_2 folder:
import shutil original = r'C:\Users\Ron\Desktop\Test_1\image.jpg' target = r'C:\Users\Ron\Desktop\Test_2\image.jpg' shutil.copyfile[original, target]
Tệp JPG bây giờ sẽ xuất hiện trong thư mục Test_2.Test_2 folder.
Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức Danh sách Python [] với sự trợ giúp của các ví dụ.
Phương thức
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]4 trả về một bản sao nông của danh sách.
Thí dụ
# mixed list
prime_numbers = [2, 3, 5]
# copying a list
numbers = prime_numbers.copy[]
print['Copied List:', numbers]
# Output: Copied List: [2, 3, 5]
Sao chép [] Cú pháp
Cú pháp của phương thức
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]4 là:
new_list = list.copy[]
Sao chép [] tham số
Phương thức
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]4 không lấy bất kỳ tham số nào.
sao chép [] giá trị trả về
Phương thức
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]4 trả về một danh sách mới. Nó không sửa đổi danh sách ban đầu.
Ví dụ: Sao chép danh sách
# mixed list
my_list = ['cat', 0, 6.7]
# copying a list
new_list = my_list.copy[]
print['Copied List:', new_list]
Đầu ra
Copied List: ['cat', 0, 6.7]
Nếu bạn sửa đổi new_list trong ví dụ trên, my_list sẽ không được sửa đổi.
Danh sách bản sao bằng cách sử dụng =
Chúng tôi cũng có thể sử dụng toán tử
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]8 để sao chép danh sách. Ví dụ,
old_list = [1, 2, 3] new_list = old_list
Howerver, có một vấn đề với việc sao chép danh sách theo cách này. Nếu bạn sửa đổi new_list, old_list cũng được sửa đổi. Đó là bởi vì danh sách mới đang tham khảo hoặc chỉ vào cùng một đối tượng Old_list.
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]0
Đầu ra
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]1
Tuy nhiên, nếu bạn cần danh sách ban đầu không thay đổi khi danh sách mới được sửa đổi, bạn có thể sử dụng phương thức
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]4.
Hướng dẫn liên quan: Python SPOPE SPOPE so với bản sao sâu Python Shallow Copy Vs Deep Copy
Ví dụ: Sao chép danh sách bằng cách sử dụng cú pháp cắt lát
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]2
Đầu ra
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]3