Hướng dẫn python shutil - potthon đã nói đùa

Nội dung chính

  • Copy nội dung file trong python | shutil.copyfile
  • Copy nội dung file kèm Permission trong python | shutil.copy
  • Copy toàn bộ file trong thư mục | shutil.copytree
  • Tổng kết và thực hành

Hướng dẫn cách copy file trong python. Bạn sẽ học được các cách sao chép file trong python như copy nội dung file, copy nội dung file kèm quyền truy cập, copy nội dung file kèm quyền truy cập và metadata. Bạn cũng sẽ học được cách sử dụng các chức năng copy tương ứng của module shutil như shutil.copyfile, shutil.copy, shutil.copy2 và shutil.copytree sau bài học này.copy file trong python. Bạn sẽ học được các cách sao chép file trong python như copy nội dung file, copy nội dung file kèm quyền truy cập, copy nội dung file kèm quyền truy cập và metadata. Bạn cũng sẽ học được cách sử dụng các chức năng copy tương ứng của module shutil như shutil.copyfile, shutil.copy, shutil.copy2 và shutil.copytree sau bài học này.

Chúng ta có thể sử dụng các chức năng copy sau đây trong module shutil để copy file trong python:

HàmCú phápChức năng
shutil.copyfile copyfile(src, dst) Copy nội dung một file
shutil.copy copy(src, dst) Copy nội dung một file kèm quyền truy cập
shutil.copy2 copy2(src, dst) Copy nội dung một file kèm Permission và Metadata
shutil.copytree copytree(src, dst) Copy toàn bộ file trong một thư mục

Trong đó:

  • src là viết tắt của source - nguồn sao chép
  • import shutil


    shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


    shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
    0 là viết tắt của destination - đích sao chép
  • import shutil


    shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


    shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
    1 là quyền truy cập vào file
  • import shutil


    shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


    shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
    2 là siêu dữ liệu - dữ liệu để mô tả file như là thời gian tạo, thời gian sửa đổi và các thông tin khác.

Copy nội dung file trong python | shutil.copyfile

Để copy nội dung file trong python, chúng ta sử dụng shutil.copyfile với cú pháp sau đây:shutil.copyfile với cú pháp sau đây:

import shutil

shutil.copyfile( src , dst )

Trong đó:

  • src là viết tắt của source - nguồn sao chép
  • import shutil


    shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


    shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
    0 là viết tắt của destination - đích sao chép

import shutil


shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
1 là quyền truy cập vào file

  |
  |--dir1
  |  |
  |  |--sample.txt
  |
  |--dir2
  |
  |--copyfile_sample.py

import shutil


shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
2 là siêu dữ liệu - dữ liệu để mô tả file như là thời gian tạo, thời gian sửa đổi và các thông tin khác.

import shutil


shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')

Để copy nội dung file trong python, chúng ta sử dụng shutil.copyfile với cú pháp sau đây:

  |
  |--dir1
  |  |
  |  |--sample.txt
  |  |--sample_copy1.txt
  |
  |--dir2
  |  |--sample_copy2.txt
  |
  |--copyfile_sample.py

import shutil

Copy nội dung file kèm Permission trong python | shutil.copy

shutil.copyfile( src , dst )shutil.copyfile chúng ta chỉ có thể copy nội dung file. Để copy nội dung file kèm cả Permission - quyền hạn truy cập file trong python, chúng ta sử dụng shutil.copy với cú pháp sau đây:

import shutil

shutil.copyfile( src , dst )

Trong đó:

  • src là viết tắt của source - nguồn sao chép
  • import shutil


    shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


    shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
    0 là viết tắt của destination - đích sao chép

import shutil


shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
1 là quyền truy cập vào file

import shutil

shutil.copy('./file01.txt', './sample')

shutil.copy('./file01.txt', './sample/file01.txt')

import shutil


shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
2 là siêu dữ liệu - dữ liệu để mô tả file như là thời gian tạo, thời gian sửa đổi và các thông tin khác.

import shutil

shutil.copy('./file01.txt', './sample/file02.txt')

Để copy nội dung file trong python, chúng ta sử dụng shutil.copyfile với cú pháp sau đây:

import shutil
Để copy nội dung file kèm Permission và Metadata trong python, chúng ta cần dùng tới shutil.copy2 với cú pháp sau đây:

import shutil

shutil.copyfile( src , dst )

src là đường dẫn của file nguồn, ví dụ như

import shutil


shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
4shutil.copy:

  • src là viết tắt của source - nguồn sao chép
  • import shutil


    shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


    shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
    0 là viết tắt của destination - đích sao chép

import shutil


shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
1 là quyền truy cập vào fileshutil.copy2 cũng tương tự shutil.copy như ví dụ sau:

import shutil


shutil.copy2('./file01.txt', './sample/file01.txt')

shutil.copy2('./file01.txt', './sample')


shutil.copy2('./file01.txt', './sample/file03.txt')

import shutil


shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
2 là siêu dữ liệu - dữ liệu để mô tả file như là thời gian tạo, thời gian sửa đổi và các thông tin khác.

Copy toàn bộ file trong thư mục | shutil.copytree

Ở phần trên chúng ta đã biết cách sử dụng 3 chức năng copy để copy một file trong python.
Lưu ý rằng bạn không thể chỉ định đối số src trong ba chức năng ở trên bằng thư mục, để copy thư mục trong python, vì lỗi sẽ xảy ra như sau:

shutil.copy('./sample', './sample_02')

Traceback (most recent call last):
File "", line 1, in
File "/home/anaconda3/envs/python3.7/lib/python3.7/shutil.py", line 245, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/home/anaconda3/envs/python3.7/lib/python3.7/shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
IsADirectoryError: [Errno 21] Is a directory: './sample'

Nếu bạn muốn copy toàn bộ file trong thư mục, hãy sử dụng tới shutil.copytree với cú pháp sau đây:

import shutil

shutil.copytree( src , dst )

Trong đó

  • src là đường dẫn của thư mục nguồn, ví dụ như
    import shutil

    shutil.copy('./file01.txt', './sample/file02.txt')
    0
  • import shutil


    shutil.copyfile('./dir1/sample.txt','./dir1/sample_copy1.txt')


    shutil.copyfile('./dir1/sample.txt','./dir2/sample_copy2.txt')
    0 là đường dẫn của thư mục đích, ví dụ như
    import shutil

    shutil.copy('./file01.txt', './sample/file02.txt')
    2.

Nếu thư mục đích không tồn tại, thư mục đích sẽ được tạo ra và sau đó toàn bộ file trong thư mục nguồn sẽ được copy sang thư mục đích.

import shutil

shutil.copytree('./sample', './sample_backup')

Tuy nhiên nếu như thư mục đích đó đã tồn tại, lỗi

import shutil

shutil.copy('./file01.txt', './sample/file02.txt')
3 sẽ bị trả về:

Traceback (most recent call last):
File "check.py", line 3, in
shutil.copytree('./sample', './sample_backup')
File "\home\anaconda3\Anaconda3\lib\shutil.py", line 554, in copytree
return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks,
File "\home\anaconda3\Anaconda3\lib\shutil.py", line 455, in _copytree
os.makedirs(dst, exist_ok=dirs_exist_ok)
File "\home\anaconda3\Anaconda3\lib\os.py", line 223, in makedirs
mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: './sample_backup'

Tổng kết và thực hành

Trên đây Kiyoshi đã hướng dẫn bạn về cách copy file trong python rồi. Để nắm rõ nội dung bài học hơn, bạn hãy thực hành viết lại các ví dụ của ngày hôm nay nhé.

Và hãy cùng tìm hiểu những kiến thức sâu hơn về python trong các bài học tiếp theo.

Viết bởi Kiyoshi. Đã đăng ký bản quyền tác giả tại Creativecommons và DMCA

Bài viết liên quan

Hãy chia sẻ và cùng lan tỏa kiến thức lập trình Nhật Bản tại Việt Nam!

HOME>> >>

Profile
Hướng dẫn python shutil - potthon đã nói đùa

Tác giả : Kiyoshi (Chis Thanh)

Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.