Đầ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:
3 Thao tác các đối tượng đã mở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
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]
4 Sao chép và đổi tênIn [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
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
5 Sao chép mà không cần tính toán siêu dữ liệuIn [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
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
6 bản sao với siêu dữ liệuIn [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
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
7In [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
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
Sao chép các tệp bằng phương pháp
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]
27Phương thức
28 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 thức 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]
29 với một tập hợp các đối số tương tự.: –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]
30: Nếu giá trị này là & nbsp; ________ 131 & nbsp; thì tất cả các kết thúc dòng sẽ được chuyển đổi thành & nbsp; ____ 132 & nbsp; như được mô tả cho & nbsp;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]
- Trong hướng dẫn Python này, bạn sẽ học cách sử dụng các chức năng khác nhau có sẵn trong các mô -đun HĐH, SHOTIL và quy trình con để sao chép các 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ách sao chép các tệp trong Python bằng cách sử dụng mô -đun SHOWIL
Mô -đun HĐH và quá trình phụ để sao chép các tệp bằng cách sử dụng lệnh shell hệ điều hành cơ bản [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
2 trong Windows hoặc 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
3 trong Unix.]
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
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
Cách sao chép tất cả các tệp từ thư mục
- Sao chép toàn bộ thư mục một cách đệ quy
Các bước để sao chép một tệp trong Python
An absolute path contains the complete directory list required to locate the file. For example,
5 is an absolute path to discover the samples.txt.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
- Python cung cấp hỗ trợ mạnh mẽ cho việc xử lý tệp. Chúng ta có thể sao chép các tệp đơn và nhiều tệp bằng các phương thức khác nhau và phương thức được sử dụng phổ biến nhất là phương thức
5. Các bước dưới đây cho thấy cách sao chép một tệp từ thư mục này sang thư mục khác.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
Tìm đường dẫn của một tệp
First, import the shutil module and Pass a source file path and destination directory path to the
7 function.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
- 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 hoàn chỉnh cần thiết để định vị tệp. Ví dụ: & nbsp; ________ 45 & nbsp; là một đường dẫn tuyệt đối để khám phá các mẫu.txt.
Sử dụng hàm SOWL.Copy []
- Mô-đun SHOTIL cung cấp một số chức năng để thực hiện các hoạt động cấp cao trên các tệp và bộ sưu tập tệp. Hàm
8 Trong mô -đun này được sử dụng để sao chép các tệp từ thư mục này sang thư mục khác.copytree[] function to copy entire directoryIn [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
Sử dụng hàm Os.ListDir [] và SOWLIL COPY [] để sao chép tất cả các tệp
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 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
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 cách sử dụng vòng lặp và sao chép từng tệp bằng hàm 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
8.
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
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
Sử dụng hàm copytree [] để sao chép toàn bộ thư mục
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']
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']
0 sao chép đệ quy toàn bộ cây thư mục bắt nguồn từ 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']
1 vào thư mục có tê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']
2 và trả lại thư mục đích
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']
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']
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']
Ví dụ: Sao chép tệp đơn
- Trong ví dụ này, chúng tôi đang sao chép tệp lợi nhuận từ thư mục báo cáo sang thư mục tài khoản.
- 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
- Nhận 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ề a & nbsp; list & nbsp; chứa tên của các tệp và thư mục trong thư mục đã cho.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
Example::
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]
Output::
copied expense.txt copied profit.txt copied revenue.txt
Lặp lại trong danh sách bằng cách sử dụng một vòng lặp để lấy tên tệp riêng lẻ
Trong mỗi lần lặp, kết hợp tên tệp hiện tại với đường dẫn thư mục nguồn
shutil.copytree[src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
- Bây giờ sử dụng phương thức
5 để sao chép tệp hiện tại vào đường dẫn thư mục đích.recursively copy an entire directory tree rooted atIn [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
1 to a directory namedimport 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']
2 and return the destination directory.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 toàn bộ thư mục
- Sử dụng tham số
0 để vượt qua bất kỳ trong bốn chức năng mà mô -đun Shutil cung cấp để sao chép các tệ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]
Ví dụ: Hãy để Lừa vào 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.: Let’s to how to copy the report directory and all its content into an account directory.
import shutil
source_dir = r"E:\demos\files\reports"
destination_dir = r"E:\demos\files\account"
shutil.copytree[source_dir, destination_dir]
Mô -đun Shutil để sao chép tệp
Mô-đun Python Shutil cung cấp nhiều chức năng cao cấp để thực hiện sao chép và xóa các tệp.
Các chức năng này cung cấp một cách tối ưu hóa để sao chép các tệp và do đó tiết kiệm thời gian trong việc thực hiện nhiệm vụ không cần thiết là mở, đọc, viết và đóng các tệp khi không cần xử lý trong tệp đó.
Mô -đun SHOTIL cung cấp bốn chức năng sau để sao chép các tệp.offers the following four functions to copy files.
1 | Sao chép đối tượng tệp từ nguồn đến đích. [Cần mở cả đường dẫn nguồn và đích] |
2 | Sao chép nội dung của một tệp vào tệp khác. Siêu dữ liệu của tệp sẽ không được sao chép. |
5 | Sao 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. |
6 | Sao 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 đích |
Để quyết định sử dụng chức năng nào để sao chép tệp, chúng tôi phải xem xét các điểm sau.consider the following points.
- Ví dụ: nếu ứng dụng của bạn sử dụng các tài nguyên được chia sẻ, bạn nên sao chép một 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ột mã sẽ chạy trên một hệ điều hành khác mà không có bất kỳ vấn đề nào.
- Chức năng tốn 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, viết và đóng tệp. Vì vậ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 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
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 copyFile [] được sử dụng để sao chép nội dung của một tệp sang tệp khác. Siêu dữ liệu của tệp sẽ không được sao chép.
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]
0
6: Vị trí tệp nguồn [đường dẫn] có nội dung cần được sao ché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]
7: Vị trí tệp đích [đường dẫn] trong đó nội dung từ tệp nguồn sẽ được dá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]
8: Giá trị mặc định cho điều này là đúng. Nếu đó là 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ệ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]
Lưu ý: Siêu dữ liệu của tệp sẽ không được sao chép trong khi sử dụng phương thức này.: The metadata of the file will not be copied while using this method.
Thí dụ::
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]
1Mã của chúng tôi đã sao chép tệp ‘lợi nhuận.txt, trong thư mục đích. Dưới đây là danh sách các tệp có trong thư mục đích:
- profit.txt
- revenue.txt
Sử dụng chức năng & nbsp; ____ ____ 69 & nbsp; để 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ọngimportant points
- Cả nguồn và đích phải là một tập tin. Nếu đích là một thư mục thì
copied expense.txt copied profit.txt copied revenue.txt
0 sẽ được nâng lên. - Nếu nguồn và đích trỏ đến cùng một tệp thì
copied expense.txt copied profit.txt copied revenue.txt
1 sẽ được nâng lên. - Nếu đích đến nhưng với một tên khác so 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 luôn bao bọc mã của bạn trong khối Ext-Except. Tham khảo ví dụ sau.avoid the above errors always wrap your code in the try-except block. Refer the following example.
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]
2Đầu ra
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]
3Phương pháp 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
5
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
Mô -đun
copied expense.txt copied profit.txt copied revenue.txt3 có thêm một phương thức để sao chép tệp được gọi là phương thức
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
8 sẽ sao chép nội dung của tệp nguồn vào tệp đích.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]
4Tương tự như phương thức
copied expense.txt copied profit.txt copied revenue.txt5 Phương thức này sẽ ném ‘
copied expense.txt copied profit.txt copied revenue.txt1‘ Nếu nguồn và đích chỉ vào cùng một tệp.
Hãy xem xét ví dụ sau đây chúng tôi cố gắng sao chép tệp ‘lợi nhuận.txt, từ nguồ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']
1 sang thư mục đích 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']
2.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]
5Lưu ý: Nếu không tìm thấy tệp nguồn trong vị trí thì phương thức sao chép [] có thể được thực thi và một copied expense.txt
copied profit.txt
copied revenue.txt
9 sẽ được nâng lên. Nếu thư mục đích không thể ghi được thì ‘
0 sẽ được nâng lên.: If the source file is
not found in the location then the copy[] method can’t be executed and an shutil.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.txt9 will be raised. If the destination folder is not writable then ‘
shutil.copytree[src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False
0‘ will be raised.Copy [] vs copyFiles []
Trong khi các phương thức
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
8 và copied expense.txt copied profit.txt copied revenue.txt5 trong mô -đun SHOTIL hoạt động theo cùng một cách trong việc sao chép các 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úng.
Nếu đường dẫn đích là ‘tệp hoặc‘ thư mục, thì phương thức
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
8 sẽ tạo một tệp trong tên của tệp nguồn.- Phương thức
8 lưu thông tin siêu dữ liệu bổ sung về các chế độ quyền từ nguồn đến tệp đích.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
copied expense.txt copied profit.txt copied revenue.txt
5 không lưu bất kỳ thông tin siêu dữ liệu nào. - Vì phương thức
8 thực hiện một cuộc gọi bổ sung để lưu các chế độ quyền; Nó chậm hơn khi so sánh với phương thức ________ 87 [].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
Phương pháp
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
8 gọi copied expense.txt copied profit.txt copied revenue.txt5 và
import shutil
source_dir = r"E:\demos\files\reports"
destination_dir = r"E:\demos\files\account"
shutil.copytree[source_dir, destination_dir]
0 bên trong việc triển khai của nó trong nội bộ. Nhưng ________ 87 [] chỉ gọi import shutil
source_dir = r"E:\demos\files\reports"
destination_dir = r"E:\demos\files\account"
shutil.copytree[source_dir, destination_dir]
2Inside việc triển khai của nóPhương pháp 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
6
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
Trong
copied expense.txt copied profit.txt copied revenue.txt3, có thêm một phương thức gọi là
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
9 có thể được sử dụng để sao chép tệp từ vị trí này sang vị trí khác.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]
6Trong khi các phương thức
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
8 và 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
9 đề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
9 có thể được sử dụng để sao chép siêu dữ liệu bổ sung là dấu thời gian của nguồn đến đích. Đây là ngoài các chế độ quyền mà phương phápIn [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
8 tiết kiệm.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
- Phương thức
9 gọi phương thứcIn [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
01 để có thông tin dấu thời gian trong khi phương thứcIn [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]
8 lưu các chế độ quyền từ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
0import shutil source_dir = r"E:\demos\files\reports" destination_dir = r"E:\demos\files\account" shutil.copytree[source_dir, destination_dir]
Các kịch bản mà phương thức này ném ‘giống nhau, tương tự như phương thức Copy []. Chúng ta có thể thấy cách sử dụng phương thức này để sao chép tệp có tên‘ sample.txt, từ nguồn đến đích.
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]
7OUPUT
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]
8Phươ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]
1
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
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
1 được sử dụng để sao chép đối tượng tệp từ nguồn đến vị trí đích.copy the file object from source to destination location.Ngoài vị trí nguồn và vị trí đích, chúng ta có thể vượt qua kích thước bộ đệm, chỉ ra phần của tệp cần được sao chép. Giá trị mặc định cho điều này là 16 kb.
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]
9
6: Vị trí tệp nguồn [đường dẫn] có nội dung cần được sao ché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]
7: Vị trí tệp đích [đường dẫn] trong đó nội dung từ tệp nguồn sẽ được dá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]
08: Kích thước bộ đệm đại diện cho số byte được giữ 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.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]
Hãy cho chúng tôi xem một ví dụ để lợi nhuận.txt từ thư mục báo cáo đến thư mục tài khoản.
Thí dụ::
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
0Sao chép các tệp bằng mô -đun hệ điều hành
Mô -đun HĐH Python cung cấp các chức năng có thể tương tác trong HĐH khác nhau. Chúng tôi phải sử dụng các lệnh sao chép cơ bản cụ thể 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 các tệp bằng phương thức 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]
09
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]
Phương thức
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]
10 mở một đường ống để thực thi lệnh mà chúng tôi chuyển trên đối tượng tệp. Phương thức này mở một đối tượng tệp trong chế độ đọc hoặc chế độ ghi. Đọc là một chế độ mặc định.In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
1
11: Lệnh sẽ được thực thi trong đối tượng tệp 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]
12: Chế độ có thể mở tệp và chế độ mặc định là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]
13.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]
14: Số byte sẽ được lưu trữ trong bộ đệm. Nếu được đề cập là 0 thì nó sẽ không tiết kiệm bất kỳ.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]
Lệnh shell bên dưới để sao chép là
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
2 cho Windows và 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
3 cho môi trường UNIX.Hãy cho chúng tôi xem cách sử dụng phương thức này để sao chép một tệp có tên Profit.txt từ thư mục nguồn sang thư mục đích.
Trên Unix::
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
2Trên Windows:
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
3Sao chép tệp bằng phương pháp 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]
17
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]
Phương thức hệ thống [] một lần nữa được sử dụng để thực thi tập lệnh trong lệnh hoặc lệnh HĐH. 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 hàm C tiêu chuẩn & nbsp; ________ 118 và có cùng giới hạn.
- 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 & nbsp; ________ 119.
- Trên Windows, giá trị trả về được trả về bởi shell hệ thống sau khi chạy & nbsp; lệnh. & Nbsp;
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
4In [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]
20: Lấy lệnh đối số duy nhất là lệnh shell bên dưới sẽ được thực thi.Hãy cho chúng tôi xem cách sử dụng phương thức này để sao chép một tệp có tên Sample.txt từ thư mục nguồn sang thư mục đích.
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
5Mô -đun phụ để sao chép tệp
Mô -đun phụ đượ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 đường ống đầu vào/đầu ra/lỗi của chúng và có được mã trả lại 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:
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
6Phương pháp 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]
21
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]
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 cuối của lệnh được thực thi.
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
7
22: Yêu cầu 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 Args.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]
- ________ 123, & nbsp; ________ 124 & nbsp; và & NBSP;
26: Nếu & nbsp; được đặt thành true, lệnh được chỉ định sẽ được thực thi thông qua shell.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]
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
8Đầu ra
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
9Sao chép các tệp bằng phương pháp 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]
27
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]
Phương thức
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]
28 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 thức 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]
29 với một tập hợp các đối số tương tự.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
0In [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]
30: Nếu giá trị này là & nbsp; ________ 131 & nbsp; thì tất cả các kết thúc dòng sẽ được chuyển đổi thành & nbsp; ____ 132 & nbsp; như được mô tả cho & nbsp;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
1Đầu ra
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
9