Mô-đun Shutil trong Python cung cấp một loạt các hoạt động và chức năng cấp cao để làm việc với các tệp và bộ sưu tập tệp. Các chức năng được cung cấp là sao chép tệp, di chuyển tệp, sử dụng đĩa, xóa thư mục, v.v.
Danh sách các chức năng được đề cập trong bài viết này
- tập tin sao chép
- chế độ sao chép
- sao chép
- sao chép
- sao chép2
- sao chép
- rmtree
- di chuyển
- disk_usage
Chức năng này được sử dụng để sao chép nội dung của tệp hoặc đối tượng giống như tệp từ nguồn đến đích
cú pháp
copyfileobj[fsrc, fdst,length]
Tham số
- fsrc. Một đối tượng giống như tệp của tệp nguồn
- fdst. Một đối tượng giống như tệp của tệp đích
- chiều dài [tùy chọn]. Một số nguyên biểu thị kích thước bộ đệm
loại trả lại. Không có loại trả lại
Ví dụ
#importing shutil module
import shutil
src="Your source file name or path of the file."
dst="Your destination file name or path of the file."
#opening the source file in read mode and get the file object in fsrc
fsrc=open[src,'r']
#opening the destination file in write mode and get the file object in fdst
fdst=open[dst,'w']
#using copyfileobj to copy file object from fsrc to fdst
shutil.copyfileobj[fsrc, fdst]
#closing file objects
f1.close[]
f2.close[]
bản saotệpChức năng này được sử dụng để sao chép nội dung của tệp nguồn sang tệp đích theo cách hiệu quả nhất có thể. Chức năng này không sao chép siêu dữ liệu của tệp nguồn sang tệp đích
cú pháp
copyfile[src, dst, *, follow_symlinks=True]
Thông số
- src. Một chuỗi chứa đường dẫn của tệp nguồn
- dst. Một chuỗi chứa đường dẫn của tệp đích
- follow_symlinks [tùy chọn]. Tham số này đại diện cho liên kết tượng trưng. Giá trị mặc định của tham số này là True. Nếu nó được đặt thành Sai và nguồn đại diện cho một liên kết tượng trưng thì thay vì tạo một tệp, một liên kết tượng trưng mới sẽ được tạo
Ghi chú. '*' trong danh sách tham số cho biết rằng tất cả các tham số sau đây chỉ là tham số từ khóa và chúng có thể được cung cấp bằng tên của chúng, không phải dưới dạng tham số vị trí
loại trả lại. Một chuỗi đại diện cho đường dẫn của tệp mới được tạo
Ví dụ
import shutil
imort os
pth="path to folder containg destination file."
src="Path of the source file."
dst="Path to thre destination file."
#Checking destination folder before copying file.
print["Destination folder before copying:",os.listdir[path]]
#Copying the file and storing it's path in dpath
dpath=shutil.copyfile[src,dst]
print["Psth of the new file:"+dpath]
#Checking destination folder after copying file.
print["Destination folder before copying:",os.listdir[path]]
chế độ sao chépChức năng này được sử dụng để sao chép các bit quyền của tệp nguồn sang tệp đích. Nội dung và quyền sở hữu của tệp nguồn và tệp đích
không thay đổi.
cú pháp
copymode[src, dst, *, follow_symlinks=True]
Tham số
- src. Một chuỗi chứa đường dẫn của tệp nguồn
- dst. Một chuỗi chứa đường dẫn của tệp đích
- follow_symlinks [tùy chọn]. Tham số này đại diện cho liên kết tượng trưng. Giá trị mặc định của tham số này là True. Nếu nó được đặt thành Sai và nguồn đại diện cho một liên kết tượng trưng thì thay vì tạo một tệp, một liên kết tượng trưng mới sẽ được tạo
Ghi chú. '*' trong danh sách tham số cho biết rằng tất cả các tham số sau đây chỉ là tham số từ khóa và chúng có thể được cung cấp bằng tên của chúng, không phải dưới dạng tham số vị trí
loại trả lại. Không có loại trả lại
Ví dụ
import os
import shutil
src = "Path of the source file."
dst = "Path of the destination file."
#Checking the permission bits of source and destination before modecopy is used.
print["Permission bits before modecopy is used:"]
print["Source:", oct[os.stat[src].st_mode][-3:]]
print["Destination:", oct[os.stat[dest].st_mode][-3:]]
# Using copymode to copy permission bits of src to dst
shutil.copymode[src,dst]
#Checking the permission bits of destination path after using copymode function.
print["Updated permission bits of destination:", oct[os.stat[dest].st_mode][-3:]]
copystatChức năng copystat có thể nói là phiên bản tốt hơn của chức năng copymode vì nó cho phép người dùng sao chép các bit quyền, thời gian truy cập lần cuối, thời gian sửa đổi lần cuối và cờ từ tệp nguồn sang tệp đích
cú pháp
copystat[src, dst, *, follow_symlinks=True]
Thông số
- src. Một chuỗi chứa đường dẫn của tệp nguồn
- dst. Một chuỗi chứa đường dẫn của tệp đích
- follow_symlinks [tùy chọn]. Tham số này đại diện cho liên kết tượng trưng. Giá trị mặc định của tham số này là True. Nếu nó được đặt thành Sai và nguồn đại diện cho một liên kết tượng trưng thì thay vì tạo một tệp, một liên kết tượng trưng mới sẽ được tạo
Ghi chú. '*' trong danh sách tham số cho biết rằng tất cả các tham số sau đây chỉ là tham số từ khóa và chúng có thể được cung cấp bằng tên của chúng, không phải dưới dạng tham số vị trí
loại trả lại. Không có loại trả lại
Ví dụ
#time module in this program is used to get the last modification time ,and last access time of a file.
import os
import shutil
import time
src = "Path of the source file."
dst = "Path of the destination file."
#checking the permission bits,last access time,last modification time and
#flags value of source and destination files before using copystat.
print["Metadata of source file before using copystat function:"]
print["Permission bits:", oct[os.stat[src].st_mode][-3:]]
print["Last access time:", time.ctime[os.stat[src].st_atime]]
print["Last modification time:", time.ctime[os.stat[src].st_mtime]]
print["Metadata of destination file before using copystat function:"]
print["Permission bits:", oct[os.stat[dest].st_mode][-3:]]
print["Last access time:", time.ctime[os.stat[dest].st_atime]]
print["Last modification time:", time.ctime[os.stat[dest].st_mtime]]
# print["User defined Flags:", os.stat[dest].st_flags]
#Using copystat to copy the permission bits last access time,
#last modification time and flags value from source to destination
shutil.copystat[src,dst]
#Checking the permission bits last access time, last modification time
#and flags value of destination.
print["Metadata of destination after using copystat:"]
print["Permission bits:", oct[os.stat[dest].st_mode][-3:]]
print["Last access time:", time.ctime[os.stat[dest].st_atime]]
print["Last modification time:", time.ctime[os.stat[dest].st_mtime]]
bản saoChức năng này được sử dụng để sao chép một tập tin từ nguồn đến đích. Nó sao chép dữ liệu tệp và chế độ cấp phép của tệp. Siêu dữ liệu khác, chẳng hạn như thời gian tạo và sửa đổi tệp, không được lưu giữ
cú pháp
________số 8Tham số
- src. Một chuỗi chứa đường dẫn của tệp nguồn
- dst. Một chuỗi chứa đường dẫn của tệp đích
- follow_symlinks [tùy chọn]. Tham số này đại diện cho liên kết tượng trưng. Giá trị mặc định của tham số này là True. Nếu nó được đặt thành Sai và nguồn đại diện cho một liên kết tượng trưng thì thay vì tạo một tệp, một liên kết tượng trưng mới sẽ được tạo
Ghi chú. '*' trong danh sách tham số cho biết rằng tất cả các tham số sau đây chỉ là tham số từ khóa và chúng có thể được cung cấp bằng tên của chúng, không phải dưới dạng tham số vị trí
loại trả lại. Nó trả về một chuỗi chứa đường dẫn của tệp mới được sao chép/tạo
Ví dụ
import os
import shutil
path="Path of the destination folder"
# List files and directories in destination folder before copying.
print["List of files and directories in destination folder/directory before copying file:",os.listdir[path]]
src="Path of source file."
dst="Path of destination file."
#Checking the file permission of the source file
perm = os.stat[source].st_mode
print["File Permission mode:", perm, "\n"]
# Copy the content of source file to destination file.
dpath=shutil.copy[src,dst]
#List of files and directories in destination folder.
print["Destination files and directory list:",os.listdir[path]]
#Checking the file permission of the destination
perms = os.stat[destination].st_mode
print["File Permissions:", perm]
#Checking the path of newly copied/created file
print["Destination path:", dpath]
bản sao2Chức năng này được sử dụng để sao chép một tập tin từ nguồn đến đích. Nó sao chép dữ liệu tệp và chế độ cấp phép của tệp cũng như siêu dữ liệu khác, chẳng hạn như thời gian tạo và sửa đổi tệp, cũng được giữ nguyên
cú pháp
#importing shutil module
import shutil
src="Your source file name or path of the file."
dst="Your destination file name or path of the file."
#opening the source file in read mode and get the file object in fsrc
fsrc=open[src,'r']
#opening the destination file in write mode and get the file object in fdst
fdst=open[dst,'w']
#using copyfileobj to copy file object from fsrc to fdst
shutil.copyfileobj[fsrc, fdst]
#closing file objects
f1.close[]
f2.close[]
0Tham số
- src. Một chuỗi chứa đường dẫn của tệp nguồn
- dst. Một chuỗi chứa đường dẫn của tệp đích
- follow_symlinks [tùy chọn]. Tham số này đại diện cho liên kết tượng trưng. Giá trị mặc định của tham số này là True. Nếu nó được đặt thành Sai và nguồn đại diện cho một liên kết tượng trưng thì thay vì tạo một tệp, một liên kết tượng trưng mới sẽ được tạo
Ghi chú. '*' trong danh sách tham số cho biết rằng tất cả các tham số sau đây chỉ là tham số từ khóa và chúng có thể được cung cấp bằng tên của chúng, không phải dưới dạng tham số vị trí
loại trả lại. Nó trả về một chuỗi chứa đường dẫn của tệp mới được sao chép/tạo
Ví dụ
#importing shutil module
import shutil
src="Your source file name or path of the file."
dst="Your destination file name or path of the file."
#opening the source file in read mode and get the file object in fsrc
fsrc=open[src,'r']
#opening the destination file in write mode and get the file object in fdst
fdst=open[dst,'w']
#using copyfileobj to copy file object from fsrc to fdst
shutil.copyfileobj[fsrc, fdst]
#closing file objects
f1.close[]
f2.close[]
1bản saoHàm này được sử dụng để sao chép đệ quy toàn bộ cây thư mục bắt nguồn từ thư mục nguồn sang thư mục đích. Thư mục đích không tồn tại và nó được tạo tại thời điểm sao chép tệp
cú pháp
#importing shutil module
import shutil
src="Your source file name or path of the file."
dst="Your destination file name or path of the file."
#opening the source file in read mode and get the file object in fsrc
fsrc=open[src,'r']
#opening the destination file in write mode and get the file object in fdst
fdst=open[dst,'w']
#using copyfileobj to copy file object from fsrc to fdst
shutil.copyfileobj[fsrc, fdst]
#closing file objects
f1.close[]
f2.close[]
2Thông số
- src. Một chuỗi chứa đường dẫn của thư mục nguồn
- dst. Một chuỗi chứa đường dẫn của thư mục đích
- liên kết tượng trưng. Nếu các liên kết tượng trưng là đúng, các liên kết tượng trưng trong cây nguồn được biểu diễn dưới dạng các liên kết tượng trưng trong cây mới và siêu dữ liệu của các liên kết ban đầu sẽ được sao chép trong phạm vi nền tảng cho phép;
- phớt lờ. Nếu bỏ qua được đưa ra, nó phải là một hàm có thể gọi được sẽ nhận làm đối số của nó, thư mục đang được truy cập bởi copytree[] và danh sách nội dung của nó, như được trả về bởi os. listdir[]. Vì copytree[] được gọi theo cách đệ quy, nên có thể gọi bỏ qua sẽ được gọi một lần cho mỗi thư mục được sao chép
- copy_function. Giá trị mặc định của tham số này là copy2. Chúng ta có thể sử dụng chức năng sao chép khác như copy, copytree, v.v. cho tham số này
- bỏ qua_dangling_symlinks. Giá trị của nó khi được đặt thành True được sử dụng để tắt tiếng đối với ngoại lệ được đưa ra nếu tệp được trỏ bởi liên kết tượng trưng không tồn tại
Loại trả lại. Nó trả về một chuỗi chứa đường dẫn của thư mục mới được tạo
Ví dụ
#importing shutil module
import shutil
src="Your source file name or path of the file."
dst="Your destination file name or path of the file."
#opening the source file in read mode and get the file object in fsrc
fsrc=open[src,'r']
#opening the destination file in write mode and get the file object in fdst
fdst=open[dst,'w']
#using copyfileobj to copy file object from fsrc to fdst
shutil.copyfileobj[fsrc, fdst]
#closing file objects
f1.close[]
f2.close[]
3rmcâyHàm này dùng để xóa toàn bộ cây thư mục đã cho trong đường dẫn
cú pháp
#importing shutil module
import shutil
src="Your source file name or path of the file."
dst="Your destination file name or path of the file."
#opening the source file in read mode and get the file object in fsrc
fsrc=open[src,'r']
#opening the destination file in write mode and get the file object in fdst
fdst=open[dst,'w']
#using copyfileobj to copy file object from fsrc to fdst
shutil.copyfileobj[fsrc, fdst]
#closing file objects
f1.close[]
f2.close[]
4Tham số
- con đường. Một chuỗi chứa đường dẫn của tệp/thư mục cần xóa
- bỏ qua_errors. Nếu ign_errors là true, các lỗi do xóa không thành công sẽ bị bỏ qua
- một lỗi. Nếu ign_errors là sai hoặc bị bỏ qua, những lỗi như vậy sẽ được xử lý bằng cách gọi một trình xử lý được chỉ định bởi onerror
loại trả lại. Không có giá trị trả lại
Ví dụ
#importing shutil module
import shutil
src="Your source file name or path of the file."
dst="Your destination file name or path of the file."
#opening the source file in read mode and get the file object in fsrc
fsrc=open[src,'r']
#opening the destination file in write mode and get the file object in fdst
fdst=open[dst,'w']
#using copyfileobj to copy file object from fsrc to fdst
shutil.copyfileobj[fsrc, fdst]
#closing file objects
f1.close[]
f2.close[]
5 di chuyểnHàm này được sử dụng để di chuyển đệ quy một tệp hoặc thư mục [src] đến một vị trí khác [dst]. Nếu đích đến là một thư mục hiện có, thì src sẽ được di chuyển vào bên trong thư mục đó. Nếu đích đã tồn tại nhưng không phải là một thư mục, nó có thể bị ghi đè tùy thuộc vào hệ điều hành. đổi tên [] ngữ nghĩa
cú pháp
#importing shutil module
import shutil
src="Your source file name or path of the file."
dst="Your destination file name or path of the file."
#opening the source file in read mode and get the file object in fsrc
fsrc=open[src,'r']
#opening the destination file in write mode and get the file object in fdst
fdst=open[dst,'w']
#using copyfileobj to copy file object from fsrc to fdst
shutil.copyfileobj[fsrc, fdst]
#closing file objects
f1.close[]
f2.close[]
6Thông số
- src. Một chuỗi chứa đường dẫn của thư mục/tệp nguồn
- dst. Một chuỗi chứa đường dẫn của thư mục/tệp đích
- copy_function. Giá trị mặc định của tham số này là copy2. Chúng ta có thể sử dụng chức năng sao chép khác như copy, copytree, v.v. cho tham số này
loại trả lại. Một chuỗi đại diện cho đường dẫn đến tệp/thư mục mới được tạo/di chuyển
Ví dụ
#importing shutil module
import shutil
src="Your source file name or path of the file."
dst="Your destination file name or path of the file."
#opening the source file in read mode and get the file object in fsrc
fsrc=open[src,'r']
#opening the destination file in write mode and get the file object in fdst
fdst=open[dst,'w']
#using copyfileobj to copy file object from fsrc to fdst
shutil.copyfileobj[fsrc, fdst]
#closing file objects
f1.close[]
f2.close[]
7disk_usageHàm này được sử dụng để lấy số liệu thống kê về mức sử dụng ổ đĩa về đường dẫn đã cho dưới dạng một bộ được đặt tên với các thuộc tính tổng, đã sử dụng và còn trống, là tổng dung lượng, đã sử dụng và còn trống, tính bằng byte
cú pháp
#importing shutil module
import shutil
src="Your source file name or path of the file."
dst="Your destination file name or path of the file."
#opening the source file in read mode and get the file object in fsrc
fsrc=open[src,'r']
#opening the destination file in write mode and get the file object in fdst
fdst=open[dst,'w']
#using copyfileobj to copy file object from fsrc to fdst
shutil.copyfileobj[fsrc, fdst]
#closing file objects
f1.close[]
f2.close[]
8Tham số
- con đường. Một chuỗi đại diện cho đường dẫn của một tập tin hoặc thư mục
loại trả lại. Hàm này trả về một bộ có tên với các thuộc tính total, used và free là tổng dung lượng, used và free, tính bằng byte
Ví dụ
#importing shutil module
import shutil
src="Your source file name or path of the file."
dst="Your destination file name or path of the file."
#opening the source file in read mode and get the file object in fsrc
fsrc=open[src,'r']
#opening the destination file in write mode and get the file object in fdst
fdst=open[dst,'w']
#using copyfileobj to copy file object from fsrc to fdst
shutil.copyfileobj[fsrc, fdst]
#closing file objects
f1.close[]
f2.close[]
9Với bài viết này tại OpenGenus, bạn sẽ có ý tưởng hay về cách sử dụng mô-đun Shutil trong Python. Sử dụng nó trong công việc của bạn và tận hưởng