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.
Đầu tiên, tôi đã thực hiện một bảng cheat đầy đủ các phương thức Shutil để bạn tham khảo.
shutil_methods =
{'copy':['shutil.copyfileobj',
'shutil.copyfile',
'shutil.copymode',
'shutil.copystat',
'shutil.copy',
'shutil.copy2',
'shutil.copytree',],
'move':['shutil.rmtree',
'shutil.move',],
'exception': ['exception shutil.SameFileError',
'exception shutil.Error'],
'others':['shutil.disk_usage',
'shutil.chown',
'shutil.which',
'shutil.ignore_patterns',]
}
Thứ hai, giải thích các phương thức sao chép trong exmaples:
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 Thao tác các đối tượng đã mở
In [3]: src = '~/Documents/Head+First+SQL.pdf'
In [4]: dst = '~/desktop'
In [5]: shutil.copyfileobj[src, dst]
AttributeError: 'str' object has no attribute 'read'
#copy the file object
In [7]: with open[src, 'rb'] as f1,open[os.path.join[dst,'test.pdf'], 'wb'] as f2:
...: shutil.copyfileobj[f1, f2]
In [8]: os.stat[os.path.join[dst,'test.pdf']]
Out[8]: os.stat_result[st_mode=33188, st_ino=8598319475, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067347, st_mtime=1516067335, st_ctime=1516067345]
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 bản sao và đổi tên
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
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 Sao chép mà không cần tính toán siêu dữ liệu
In [10]: shutil.copy[src, dst]
Out[10]: ~/desktop/Head+First+SQL.pdf'
#check their metadata
In [25]: os.stat[src]
Out[25]: os.stat_result[st_mode=33188, st_ino=597749, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516066425, st_mtime=1493698739, st_ctime=1514871215]
In [26]: os.stat[os.path.join[dst, 'Head+First+SQL.pdf']]
Out[26]: os.stat_result[st_mode=33188, st_ino=8598313736, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516066427, st_mtime=1516066425, st_ctime=1516066425]
# st_atime,st_mtime,st_ctime changed
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 Bản sao với siêu dữ liệu
In [30]: shutil.copy2[src, dst]
Out[30]: ~/desktop/Head+First+SQL.pdf'
In [31]: os.stat[src]
Out[31]: os.stat_result[st_mode=33188, st_ino=597749, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067055, st_mtime=1493698739, st_ctime=1514871215]
In [32]: os.stat[os.path.join[dst, 'Head+First+SQL.pdf']]
Out[32]: os.stat_result[st_mode=33188, st_ino=8598313736, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067063, st_mtime=1493698739, st_ctime=1516067055]
# Preseved st_mtime
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
Sao chép đệ quy toàn bộ cây thư mục bắt nguồn từ SRC, trả lại thư mục đích