Python nhập tệp văn bản từ thư mục khác

Python có một số mô-đun và chức năng tích hợp để xử lý tệp. Các chức năng này được trải rộng trên một số mô-đun, chẳng hạn như

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
02,
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
03,
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
04 và
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05, để kể tên một số. Bài viết này tập hợp tại một nơi nhiều hàm bạn cần biết để thực hiện các thao tác phổ biến nhất trên tệp trong Python

Show

Trong hướng dẫn này, bạn sẽ học cách

  • Truy xuất thuộc tính tệp
  • Tạo thư mục
  • Khớp các mẫu trong tên tệp
  • Duyệt cây thư mục
  • Tạo các tệp và thư mục tạm thời
  • Xóa tệp và thư mục
  • Sao chép, di chuyển hoặc đổi tên tệp và thư mục
  • Tạo và trích xuất kho lưu trữ ZIP và TAR
  • Mở nhiều tệp bằng mô-đun
    with open('data.txt', 'w') as f:
        data = 'some data to be written to the file'
        f.write(data)
    
    06

Tiền thưởng miễn phí. 5 Suy nghĩ về Làm chủ Python, một khóa học miễn phí dành cho các nhà phát triển Python cho bạn thấy lộ trình và tư duy mà bạn sẽ cần để đưa các kỹ năng Python của mình lên một tầm cao mới

Mô hình “với open(…) as…” của Python

Đọc và ghi dữ liệu vào tệp bằng Python khá đơn giản. Để làm điều này, trước tiên bạn phải mở tệp ở chế độ thích hợp. Đây là một ví dụ về cách sử dụng mẫu “with open(…) as…” của Python để mở một tệp văn bản và đọc nội dung của nó

with open('data.txt', 'r') as f:
    data = f.read()

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
07 lấy tên tệp và chế độ làm đối số.
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
08 mở tệp ở chế độ chỉ đọc. Để ghi dữ liệu vào một tệp, thay vào đó hãy chuyển vào
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
09 làm đối số

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)

Trong các ví dụ trên,

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
07 mở tệp để đọc hoặc ghi và trả về một thẻ xử lý tệp (
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
11 trong trường hợp này) cung cấp các phương thức có thể được sử dụng để đọc hoặc ghi dữ liệu vào tệp. Hãy xem Đọc và ghi tệp bằng Python và Làm việc với I/O tệp bằng Python để biết thêm thông tin về cách đọc và ghi vào tệp

Loại bỏ các quảng cáo

Lấy danh sách thư mục

Giả sử thư mục làm việc hiện tại của bạn có một thư mục con tên là

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
12 có các nội dung sau

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt

Mô-đun

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
02 tích hợp có một số chức năng hữu ích có thể được sử dụng để liệt kê nội dung thư mục và lọc kết quả. Để có danh sách tất cả các tệp và thư mục trong một thư mục cụ thể trong hệ thống tệp, hãy sử dụng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14 trong các phiên bản cũ của Python hoặc
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 trong Python 3. x.
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 là phương pháp ưa thích để sử dụng nếu bạn cũng muốn nhận các thuộc tính của tệp và thư mục, chẳng hạn như kích thước tệp và ngày sửa đổi

Danh sách thư mục trong các phiên bản Python kế thừa

Trong các phiên bản Python trước Python 3,

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14 là phương pháp được sử dụng để lấy danh sách thư mục

>>>

>>> import os
>>> entries = os.listdir('my_directory/')

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14 trả về một danh sách Python chứa tên của các tệp và thư mục con trong thư mục được cung cấp bởi đối số đường dẫn

>>>

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']

Một danh sách thư mục như thế không dễ đọc. In ra đầu ra của lệnh gọi tới

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14 bằng cách sử dụng vòng lặp giúp dọn dẹp mọi thứ

>>>

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir

Danh sách thư mục trong các phiên bản Python hiện đại

Trong các phiên bản hiện đại của Python, một giải pháp thay thế cho

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14 là sử dụng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 và
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 đã được giới thiệu trong Python 3. 5 và được ghi lại trong PEP 471.
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 trả về một iterator thay vì một danh sách khi được gọi

>>>

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
25 trỏ tới tất cả các mục trong thư mục hiện tại. Bạn có thể lặp lại nội dung của iterator và in ra tên tệp

import os

with os.scandir('my_directory/') as entries:
    for entry in entries:
        print(entry.name)

Ở đây,

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 được sử dụng cùng với câu lệnh
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
27 vì nó hỗ trợ giao thức quản lý ngữ cảnh. Sử dụng trình quản lý bối cảnh sẽ đóng trình vòng lặp và tự động giải phóng các tài nguyên đã thu được sau khi sử dụng hết trình vòng lặp. Kết quả là một bản in tên tệp trong
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
28 giống như bạn đã thấy trong ví dụ
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14

sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir

Một cách khác để lấy danh sách thư mục là sử dụng mô-đun

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05

from pathlib import Path

entries = Path('my_directory/')
for entry in entries.iterdir():
    print(entry.name)

Các đối tượng được trả về bởi

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
31 là đối tượng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
32 hoặc
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
33 tùy thuộc vào hệ điều hành

Các đối tượng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22 có một phương thức
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
35 để tạo một trình vòng lặp của tất cả các tệp và thư mục trong một thư mục. Mỗi mục do
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
35 tạo ra chứa thông tin về tệp hoặc thư mục như tên và thuộc tính tệp của nó.
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05 lần đầu tiên được giới thiệu trong Python 3. 4 và là một bổ sung tuyệt vời cho Python cung cấp giao diện hướng đối tượng cho hệ thống tệp

Trong ví dụ trên, bạn gọi

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22 và truyền đối số đường dẫn cho nó. Tiếp theo là cuộc gọi đến
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
35 để lấy danh sách tất cả các tệp và thư mục trong
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
12

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05 cung cấp một tập hợp các lớp bao gồm hầu hết các hoạt động phổ biến trên các đường dẫn theo cách hướng đối tượng dễ dàng. Sử dụng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05 sẽ hiệu quả hơn nếu không muốn nói là sử dụng các chức năng trong
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
02. Một lợi ích khác của việc sử dụng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05 thay vì
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
02 là nó giảm số lần nhập mà bạn cần thực hiện để thao tác các đường dẫn hệ thống tệp. Để biết thêm thông tin, hãy đọc Mô-đun pathlib của Python 3. Thuần hóa hệ thống tập tin

Ghi chú. Để bắt đầu với

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05, hãy xem Khái niệm cơ bản về Python. File System Operations và các bài tập liên quan

Chạy đoạn mã trên tạo ra như sau

sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir

Sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22 hoặc
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 thay vì
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14 là cách ưa thích để nhận danh sách thư mục, đặc biệt khi bạn đang làm việc với mã cần loại tệp và thông tin thuộc tính tệp.
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22 cung cấp nhiều chức năng xử lý tệp và đường dẫn có trong
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
02 và
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
04, đồng thời các phương thức của nó hiệu quả hơn một số phương thức được tìm thấy trong các mô-đun này. Chúng tôi sẽ sớm thảo luận về cách lấy thuộc tính tệp

Đây là các chức năng liệt kê thư mục một lần nữa

Mô tả chức năng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14Trả về danh sách tất cả các tệp và thư mục trong một thư mục
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15Trả về một trình vòng lặp của tất cả các đối tượng trong một thư mục bao gồm thông tin thuộc tính tệp
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
55Trả về một trình lặp của tất cả các đối tượng trong một thư mục bao gồm thông tin thuộc tính tệp

Các hàm này trả về danh sách mọi thứ trong thư mục, bao gồm cả thư mục con. Đây có thể không phải lúc nào cũng là hành vi bạn muốn. Phần tiếp theo sẽ chỉ cho bạn cách lọc kết quả từ danh sách thư mục

Loại bỏ các quảng cáo

Liệt kê tất cả các tệp trong một thư mục

Phần này sẽ chỉ cho bạn cách in ra tên của các tệp trong một thư mục bằng cách sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14,
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 và
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22. Để lọc ra các thư mục và chỉ liệt kê các tệp từ danh sách thư mục được tạo bởi
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14, hãy sử dụng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
03

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
1

Ở đây, lệnh gọi tới ___0_______14 trả về danh sách mọi thứ trong đường dẫn đã chỉ định, sau đó danh sách đó được lọc bởi ____0_______62 để chỉ in ra các tệp chứ không phải thư mục. Điều này tạo ra đầu ra sau

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
2

Một cách dễ dàng hơn để liệt kê các tệp trong một thư mục là sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 hoặc
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
3

Sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 có ưu điểm là trông gọn gàng và dễ hiểu hơn so với sử dụng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14, mặc dù nó dài hơn một dòng mã. Gọi
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
67 trên mỗi mục trong
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
25 trả về
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
69 nếu đối tượng là một tệp. In ra tên của tất cả các tệp trong thư mục sẽ cho bạn đầu ra sau

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
2

Đây là cách liệt kê các tệp trong một thư mục bằng cách sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
5

Ở đây, bạn gọi

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
71 trên mỗi mục do
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
35 mang lại. Sản lượng được sản xuất là như nhau

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
2

Đoạn mã trên có thể ngắn gọn hơn nếu bạn kết hợp vòng lặp

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
73 và câu lệnh
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
74 vào một biểu thức trình tạo duy nhất. Dan Bader có một bài viết xuất sắc về biểu thức trình tạo và cách hiểu danh sách

Phiên bản sửa đổi trông như thế này

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
7

Điều này tạo ra chính xác đầu ra giống như ví dụ trước nó. Phần này cho thấy rằng việc lọc các tệp hoặc thư mục bằng cách sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 và
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22 mang lại cảm giác trực quan hơn và trông gọn gàng hơn so với việc sử dụng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14 kết hợp với
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
03

Liệt kê các thư mục con

Để liệt kê các thư mục con thay vì tệp, hãy sử dụng một trong các phương pháp bên dưới. Đây là cách sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14 và
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
80

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
8

Thao tác với các đường dẫn hệ thống tệp theo cách này có thể nhanh chóng trở nên cồng kềnh khi bạn có nhiều lệnh gọi tới

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
81. Chạy cái này trên máy tính của tôi tạo ra đầu ra sau

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
9

Đây là cách sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
0

Như trong ví dụ liệt kê tệp, ở đây bạn gọi

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
83 trên mỗi mục được trả về bởi
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15. Nếu mục nhập là một thư mục,
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
83 trả về
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
69 và tên của thư mục được in ra. Đầu ra giống như trên

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
9

Đây là cách sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
2

Gọi

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
83 trên mỗi mục của trình vòng lặp
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
89 để kiểm tra xem mục đó là một tệp hay một thư mục. Nếu mục nhập là một thư mục, tên của nó sẽ được in ra màn hình và đầu ra được tạo giống với đầu ra trong ví dụ trước

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
9

Loại bỏ các quảng cáo

Lấy thuộc tính tệp

Python giúp truy xuất các thuộc tính tệp như kích thước tệp và thời gian sửa đổi dễ dàng. Điều này được thực hiện thông qua

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
90,
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 hoặc
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 và
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22 truy xuất danh sách thư mục với các thuộc tính tệp được kết hợp. Điều này có thể hiệu quả hơn so với việc sử dụng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14 để liệt kê các tệp và sau đó lấy thông tin thuộc tính tệp cho từng tệp

Các ví dụ dưới đây cho thấy cách lấy thời gian các tệp trong

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
28 được sửa đổi lần cuối. Đầu ra tính bằng giây

>>>

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
4

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
15 trả về một đối tượng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
25. Mỗi mục trong một đối tượng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
25 có một phương thức
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
00 truy xuất thông tin về tệp hoặc thư mục mà nó trỏ tới.
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
00 cung cấp thông tin như kích thước tệp và thời gian sửa đổi lần cuối. Trong ví dụ trên, mã in ra thuộc tính
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
02, là thời gian nội dung của tệp được sửa đổi lần cuối

Mô-đun

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05 có các phương pháp truy xuất thông tin tệp tương ứng cho kết quả tương tự

>>>

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
5

Trong ví dụ trên, mã lặp qua đối tượng được trả về bởi

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
35 và truy xuất các thuộc tính tệp thông qua lệnh gọi
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
00 cho mỗi tệp trong danh sách thư mục. Thuộc tính
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
02 trả về một giá trị float đại diện cho. Để chuyển đổi các giá trị được trả về bởi
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
02 cho mục đích hiển thị, bạn có thể viết hàm trợ giúp để chuyển đổi giây thành đối tượng
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
08

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
6

Điều này trước tiên sẽ nhận được một danh sách các tệp trong

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
12 và các thuộc tính của chúng, sau đó gọi
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
10 để chuyển đổi thời gian sửa đổi lần cuối của mỗi tệp thành dạng có thể đọc được của con người.
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
10 sử dụng
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
12 để chuyển đổi thời gian tính bằng giây thành một chuỗi

Các đối số được chuyển đến

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
12 như sau

  • my_directory/
    |
    ├── sub_dir/
    |   ├── bar.py
    |   └── foo.py
    |
    ├── sub_dir_b/
    |   └── file4.txt
    |
    ├── sub_dir_c/
    |   ├── config.py
    |   └── file5.txt
    |
    ├── file1.py
    ├── file2.csv
    └── file3.txt
    
    14. ngày của tháng
  • my_directory/
    |
    ├── sub_dir/
    |   ├── bar.py
    |   └── foo.py
    |
    ├── sub_dir_b/
    |   └── file4.txt
    |
    ├── sub_dir_c/
    |   ├── config.py
    |   └── file5.txt
    |
    ├── file1.py
    ├── file2.csv
    └── file3.txt
    
    15. tháng, ở dạng viết tắt
  • my_directory/
    |
    ├── sub_dir/
    |   ├── bar.py
    |   └── foo.py
    |
    ├── sub_dir_b/
    |   └── file4.txt
    |
    ├── sub_dir_c/
    |   ├── config.py
    |   └── file5.txt
    |
    ├── file1.py
    ├── file2.csv
    └── file3.txt
    
    16. năm

Cùng với nhau, các lệnh này tạo ra đầu ra trông như thế này

>>>

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
7

Cú pháp chuyển đổi ngày và giờ thành chuỗi có thể khá khó hiểu. Để đọc thêm về nó, hãy xem trên đó. Một tài liệu tham khảo hữu ích khác dễ nhớ là http. // strftime. tổ chức/

Tạo thư mục

Sớm hay muộn, các chương trình bạn viết sẽ phải tạo các thư mục để lưu trữ dữ liệu trong đó.

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
02 và
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05 bao gồm các chức năng tạo thư mục. Chúng tôi sẽ xem xét những

Chức năng Mô tả

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
19Tạo một thư mục con duy nhất
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
20Tạo một hoặc nhiều thư mục
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
21Tạo nhiều thư mục, bao gồm các thư mục trung gian

Tạo một thư mục duy nhất

Để tạo một thư mục, hãy chuyển đường dẫn đến thư mục dưới dạng tham số cho

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
19

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
8

Nếu một thư mục đã tồn tại,

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
19 tăng
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
24. Ngoài ra, bạn có thể tạo thư mục bằng cách sử dụng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
9

Nếu đường dẫn đã tồn tại,

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
26 sẽ tạo ra một
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
24

>>>

>>> import os
>>> entries = os.listdir('my_directory/')
0

Để tránh những lỗi như thế này, hãy nắm bắt lỗi khi nó xảy ra và cho người dùng của bạn biết

>>> import os
>>> entries = os.listdir('my_directory/')
1

Ngoài ra, bạn có thể bỏ qua

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
24 bằng cách chuyển đối số
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
29 cho
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
30

>>> import os
>>> entries = os.listdir('my_directory/')
2

Điều này sẽ không gây ra lỗi nếu thư mục đã tồn tại

Loại bỏ các quảng cáo

Tạo nhiều thư mục

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
21 tương tự như
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
19. Sự khác biệt giữa hai loại này là
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
21 không chỉ có thể tạo các thư mục riêng lẻ mà còn có thể được sử dụng để tạo cây thư mục. Nói cách khác, nó có thể tạo bất kỳ thư mục trung gian cần thiết nào để đảm bảo tồn tại một đường dẫn đầy đủ

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
21 tương tự như chạy
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
35 trong Bash. Ví dụ: để tạo một nhóm các thư mục như
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
36, tất cả những gì bạn phải làm là như sau

>>> import os
>>> entries = os.listdir('my_directory/')
3

Thao tác này sẽ tạo cấu trúc thư mục lồng nhau chứa các thư mục 2018, 10 và 05

>>> import os
>>> entries = os.listdir('my_directory/')
4

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
37 tạo thư mục với quyền mặc định. Nếu bạn cần tạo các thư mục với các quyền khác nhau, hãy gọi
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
37 và chuyển sang chế độ bạn muốn các thư mục được tạo trong

>>> import os
>>> entries = os.listdir('my_directory/')
5

Điều này tạo cấu trúc thư mục

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
36 và cung cấp cho chủ sở hữu và người dùng nhóm quyền đọc, viết và thực thi. Chế độ mặc định là
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
40 và các bit cấp phép tệp của các thư mục mẹ hiện tại không bị thay đổi. Để biết thêm chi tiết về quyền truy cập tệp và cách áp dụng chế độ,

Chạy

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
41 để xác nhận rằng các quyền phù hợp đã được áp dụng

>>> import os
>>> entries = os.listdir('my_directory/')
6

Điều này in ra một cây thư mục của thư mục hiện tại.

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
41 thường được sử dụng để liệt kê nội dung của các thư mục ở định dạng giống như cây. Truyền các đối số
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
43 và
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
44 cho nó in ra tên thư mục và thông tin cấp phép tệp của chúng trong một danh sách dọc.
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
43 in ra các quyền của tệp và
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
44 làm cho
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
41 tạo ra một danh sách dọc không có dòng thụt đầu dòng

Như bạn có thể thấy, tất cả các thư mục đều có quyền

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
48. Một cách khác để tạo thư mục là sử dụng
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
30 từ
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
50

>>> import os
>>> entries = os.listdir('my_directory/')
7

Chuyển

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
51 đến
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
52 để tạo thư mục
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
53 và bất kỳ thư mục mẹ nào cần thiết để làm cho đường dẫn hợp lệ

Theo mặc định,

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
21 và
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
52 tăng
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
56 nếu thư mục đích đã tồn tại. Hành vi này có thể bị ghi đè (kể từ Python 3. 2) bằng cách chuyển
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
29 làm đối số từ khóa khi gọi từng hàm

Chạy đoạn mã trên sẽ tạo ra một cấu trúc thư mục giống như bên dưới trong một lần

>>> import os
>>> entries = os.listdir('my_directory/')
4

Tôi thích sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05 hơn khi tạo thư mục vì tôi có thể sử dụng chức năng tương tự để tạo các thư mục đơn hoặc lồng nhau

Khớp mẫu tên tệp

Sau khi nhận được danh sách các tệp trong một thư mục bằng một trong các phương pháp trên, rất có thể bạn sẽ muốn tìm kiếm các tệp khớp với một mẫu cụ thể

Đây là những phương pháp và chức năng có sẵn cho bạn

  • Các phương thức chuỗi
    my_directory/
    |
    ├── sub_dir/
    |   ├── bar.py
    |   └── foo.py
    |
    ├── sub_dir_b/
    |   └── file4.txt
    |
    ├── sub_dir_c/
    |   ├── config.py
    |   └── file5.txt
    |
    ├── file1.py
    ├── file2.csv
    └── file3.txt
    
    59 và
    my_directory/
    |
    ├── sub_dir/
    |   ├── bar.py
    |   └── foo.py
    |
    ├── sub_dir_b/
    |   └── file4.txt
    |
    ├── sub_dir_c/
    |   ├── config.py
    |   └── file5.txt
    |
    ├── file1.py
    ├── file2.csv
    └── file3.txt
    
    60
  • my_directory/
    |
    ├── sub_dir/
    |   ├── bar.py
    |   └── foo.py
    |
    ├── sub_dir_b/
    |   └── file4.txt
    |
    ├── sub_dir_c/
    |   ├── config.py
    |   └── file5.txt
    |
    ├── file1.py
    ├── file2.csv
    └── file3.txt
    
    61
  • my_directory/
    |
    ├── sub_dir/
    |   ├── bar.py
    |   └── foo.py
    |
    ├── sub_dir_b/
    |   └── file4.txt
    |
    ├── sub_dir_c/
    |   ├── config.py
    |   └── file5.txt
    |
    ├── file1.py
    ├── file2.csv
    └── file3.txt
    
    62
  • my_directory/
    |
    ├── sub_dir/
    |   ├── bar.py
    |   └── foo.py
    |
    ├── sub_dir_b/
    |   └── file4.txt
    |
    ├── sub_dir_c/
    |   ├── config.py
    |   └── file5.txt
    |
    ├── file1.py
    ├── file2.csv
    └── file3.txt
    
    63

Mỗi cái được thảo luận dưới đây. Các ví dụ trong phần này sẽ được thực hiện trên một thư mục có tên là

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
64 có cấu trúc như sau

>>> import os
>>> entries = os.listdir('my_directory/')
9

Nếu bạn đang theo dõi bằng cách sử dụng trình bao Bash, bạn có thể tạo cấu trúc thư mục trên bằng cách sử dụng các lệnh sau

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
0

Thao tác này sẽ tạo thư mục

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
65, thay đổi thành thư mục đó và sau đó tạo
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
66. Dòng tiếp theo tạo
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
67 và
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
68 trong
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
66 và dòng cuối cùng tạo tất cả các tệp khác bằng cách sử dụng phần mở rộng. Để tìm hiểu thêm về mở rộng vỏ, hãy truy cập trang web này

Loại bỏ các quảng cáo

Sử dụng các phương thức chuỗi

Python có một số phương thức tích hợp để sửa đổi và thao tác chuỗi. Hai trong số các phương pháp này,

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
70 và
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
71, rất hữu ích khi bạn đang tìm kiếm các mẫu trong tên tệp. Để làm điều này, trước tiên hãy lấy một danh sách thư mục và sau đó lặp lại nó

>>>

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
1

Đoạn mã trên tìm tất cả các tệp trong

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
65, lặp lại chúng và sử dụng
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
71 để in ra các tên tệp có phần mở rộng tệp
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
74. Chạy cái này trên máy tính của tôi tạo ra đầu ra sau

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
2

Khớp mẫu tên tệp đơn giản bằng cách sử dụng my_directory/ | ├── sub_dir/ | ├── bar.py | └── foo.py | ├── sub_dir_b/ | └── file4.txt | ├── sub_dir_c/ | ├── config.py | └── file5.txt | ├── file1.py ├── file2.csv └── file3.txt 75

Các phương thức chuỗi bị hạn chế về khả năng kết hợp của chúng.

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
75 có nhiều chức năng và phương pháp nâng cao hơn để so khớp mẫu. Chúng tôi sẽ xem xét
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
61, một chức năng hỗ trợ sử dụng các ký tự đại diện như
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
78 và
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
79 để khớp với tên tệp. Ví dụ: để tìm tất cả các tệp
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
74 trong một thư mục bằng cách sử dụng
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
75, bạn sẽ làm như sau

>>>

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
3

Điều này lặp lại danh sách các tệp trong

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
64 và sử dụng
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
83 để thực hiện tìm kiếm ký tự đại diện cho các tệp có phần mở rộng
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
74

So khớp mẫu nâng cao hơn

Giả sử bạn muốn tìm _______13_______74 tệp đáp ứng các tiêu chí nhất định. Ví dụ: bạn có thể chỉ quan tâm đến việc tìm các tệp

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
74 có chứa từ
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
87, một số nằm giữa một tập hợp các dấu gạch dưới và từ
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
88 trong tên tệp của chúng. Một cái gì đó tương tự như
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
89,
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
90 hoặc
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
91

Sử dụng

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
61, bạn có thể làm theo cách này

>>>

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
4

Tại đây, bạn chỉ in ra tên của các tệp khớp với mẫu

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
93. Dấu hoa thị trong mẫu sẽ khớp với bất kỳ ký tự nào, do đó, việc chạy này sẽ tìm thấy tất cả các tệp văn bản có tên tệp bắt đầu bằng từ
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
87 và kết thúc bằng
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
95, như bạn có thể thấy từ đầu ra bên dưới

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
5

Khớp mẫu tên tệp bằng cách sử dụng my_directory/ | ├── sub_dir/ | ├── bar.py | └── foo.py | ├── sub_dir_b/ | └── file4.txt | ├── sub_dir_c/ | ├── config.py | └── file5.txt | ├── file1.py ├── file2.csv └── file3.txt 96

Một mô-đun hữu ích khác để khớp mẫu là

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
96

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
98 trong mô-đun
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
96 hoạt động giống như
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
61, nhưng không giống như
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
61, nó coi các tệp bắt đầu bằng dấu chấm (
>>> import os
>>> entries = os.listdir('my_directory/')
02) là đặc biệt

UNIX và các hệ thống liên quan dịch các mẫu tên có ký tự đại diện như

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
79 và
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
78 thành danh sách các tệp. Điều này được gọi là toàn cầu hóa

Ví dụ: gõ _____19_______05 trong trình bao UNIX sẽ di chuyển (_______19_______06) tất cả các tệp có phần mở rộng ___19_______07 từ thư mục hiện tại sang thư mục ____19_______08. Ký tự

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
78 là ký tự đại diện có nghĩa là “bất kỳ số lượng ký tự nào” và
>>> import os
>>> entries = os.listdir('my_directory/')
10 là mẫu hình cầu. Khả năng trình bao này không khả dụng trong Hệ điều hành Windows. Mô-đun
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
96 bổ sung khả năng này trong Python, cho phép các chương trình Windows sử dụng tính năng này

Đây là một ví dụ về cách sử dụng

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
96 để tìm kiếm tất cả các tệp nguồn Python (
>>> import os
>>> entries = os.listdir('my_directory/')
07) trong thư mục hiện tại

>>>

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
6

>>> import os
>>> entries = os.listdir('my_directory/')
14 tìm kiếm tất cả các tệp có phần mở rộng
>>> import os
>>> entries = os.listdir('my_directory/')
07 trong thư mục hiện tại và trả về chúng dưới dạng danh sách.
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
96 cũng hỗ trợ các ký tự đại diện kiểu shell để khớp các mẫu

>>>

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
7

Điều này tìm thấy tất cả các tệp văn bản (

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
74) có chứa các chữ số trong tên tệp

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
2

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
96 cũng giúp dễ dàng tìm kiếm các tệp theo cách đệ quy trong các thư mục con

>>>

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
9

Ví dụ này sử dụng

>>> import os
>>> entries = os.listdir('my_directory/')
19 để tìm kiếm các tệp
>>> import os
>>> entries = os.listdir('my_directory/')
07 trong thư mục hiện tại và thư mục con. Truyền
>>> import os
>>> entries = os.listdir('my_directory/')
21 làm đối số cho
>>> import os
>>> entries = os.listdir('my_directory/')
22 làm cho nó tìm kiếm các tệp
>>> import os
>>> entries = os.listdir('my_directory/')
07 trong thư mục hiện tại và bất kỳ thư mục con nào. Sự khác biệt giữa
>>> import os
>>> entries = os.listdir('my_directory/')
19 và
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
62 là
>>> import os
>>> entries = os.listdir('my_directory/')
22 trả về một trình vòng lặp thay vì một danh sách

Chạy chương trình trên tạo ra như sau

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
0

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05 chứa các phương pháp tương tự để tạo danh sách tệp linh hoạt. Ví dụ dưới đây cho thấy cách bạn có thể sử dụng
>>> import os
>>> entries = os.listdir('my_directory/')
28 để liệt kê các loại tệp bắt đầu bằng chữ cái
>>> import os
>>> entries = os.listdir('my_directory/')
29

>>>

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
1

Gọi

>>> import os
>>> entries = os.listdir('my_directory/')
30 trả về một đối tượng trình tạo trỏ tới tất cả các tệp trong thư mục hiện tại bắt đầu bằng chữ cái
>>> import os
>>> entries = os.listdir('my_directory/')
29 trong phần mở rộng tệp của chúng

>>> import os
>>> entries = os.listdir('my_directory/')
32 tương tự như
>>> import os
>>> entries = os.listdir('my_directory/')
33 đã thảo luận ở trên. Như bạn có thể thấy,
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05 kết hợp nhiều tính năng tốt nhất của các mô-đun
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
02,
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
03 và
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
96 vào một mô-đun duy nhất, điều này làm cho việc sử dụng trở nên thú vị

Tóm lại, đây là bảng các chức năng chúng tôi đã trình bày trong phần này

FunctionDescription

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
60Kiểm tra xem một chuỗi có bắt đầu bằng một mẫu cụ thể hay không và trả về
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
69 hoặc
>>> import os
>>> entries = os.listdir('my_directory/')
40
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
59Kiểm tra xem một chuỗi có kết thúc bằng một mẫu cụ thể hay không và trả về
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
69 hoặc
>>> import os
>>> entries = os.listdir('my_directory/')
40
>>> import os
>>> entries = os.listdir('my_directory/')
44Kiểm tra xem tên tệp có khớp với mẫu đó hay không và trả về
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
69 hoặc
>>> import os
>>> entries = os.listdir('my_directory/')
40
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
62Trả về danh sách các tên tệp khớp với một mẫu3__Tìm kiếm 1________

Loại bỏ các quảng cáo

Duyệt qua các thư mục và xử lý tệp

Một nhiệm vụ lập trình phổ biến là duyệt cây thư mục và xử lý các tệp trong cây. Hãy cùng khám phá cách hàm Python tích hợp sẵn

>>> import os
>>> entries = os.listdir('my_directory/')
49 có thể được sử dụng để làm điều này.
>>> import os
>>> entries = os.listdir('my_directory/')
49 được sử dụng để tạo tên tệp trong cây thư mục bằng cách di chuyển cây từ trên xuống hoặc từ dưới lên. Với mục đích của phần này, chúng ta sẽ thao tác với cây thư mục sau

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
2

Sau đây là một ví dụ chỉ cho bạn cách liệt kê tất cả các tệp và thư mục trong cây thư mục bằng cách sử dụng

>>> import os
>>> entries = os.listdir('my_directory/')
49

>>> import os
>>> entries = os.listdir('my_directory/')
49 mặc định duyệt qua các thư mục theo cách từ trên xuống

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
3

>>> import os
>>> entries = os.listdir('my_directory/')
49 trả về ba giá trị trên mỗi lần lặp của vòng lặp

  1. Tên của thư mục hiện tại

  2. Một danh sách các thư mục trong thư mục hiện tại

  3. Danh sách các tệp trong thư mục hiện tại

Trên mỗi lần lặp, nó in ra tên của các thư mục con và tệp mà nó tìm thấy

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
4

Để duyệt qua cây thư mục theo cách từ dưới lên, hãy chuyển đối số từ khóa

>>> import os
>>> entries = os.listdir('my_directory/')
54 tới
>>> import os
>>> entries = os.listdir('my_directory/')
49

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
5

Truyền đối số

>>> import os
>>> entries = os.listdir('my_directory/')
54 sẽ làm cho
>>> import os
>>> entries = os.listdir('my_directory/')
49 in ra các tệp mà nó tìm thấy trong thư mục con trước

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
6

Như bạn có thể thấy, chương trình bắt đầu bằng cách liệt kê nội dung của các thư mục con trước khi liệt kê nội dung của thư mục gốc. Điều này rất hữu ích trong trường hợp bạn muốn xóa đệ quy các tệp và thư mục. Bạn sẽ học cách làm điều này trong các phần dưới đây. Theo mặc định,

>>> import os
>>> entries = os.listdir('my_directory/')
58 không đi xuống các liên kết tượng trưng giải quyết các thư mục. Hành vi này có thể bị ghi đè bằng cách gọi nó bằng đối số
>>> import os
>>> entries = os.listdir('my_directory/')
59

Tạo tập tin tạm thời và thư mục

Python cung cấp một mô-đun tiện dụng để tạo các tệp và thư mục tạm thời có tên là

>>> import os
>>> entries = os.listdir('my_directory/')
60

>>> import os
>>> entries = os.listdir('my_directory/')
60 có thể được sử dụng để mở và lưu trữ dữ liệu tạm thời trong một tệp hoặc thư mục trong khi chương trình của bạn đang chạy.
>>> import os
>>> entries = os.listdir('my_directory/')
60 xử lý việc xóa các tệp tạm thời khi chương trình của bạn hoàn thành chúng

Đây là cách tạo một tệp tạm thời

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
7

Bước đầu tiên là nhập

>>> import os
>>> entries = os.listdir('my_directory/')
63 từ mô-đun
>>> import os
>>> entries = os.listdir('my_directory/')
60. Tiếp theo, tạo một tệp giống như đối tượng bằng cách sử dụng phương thức
>>> import os
>>> entries = os.listdir('my_directory/')
65 bằng cách gọi nó và chuyển chế độ bạn muốn mở tệp trong đó. Điều này sẽ tạo và mở một tệp có thể được sử dụng làm vùng lưu trữ tạm thời

Trong ví dụ trên, chế độ là

>>> import os
>>> entries = os.listdir('my_directory/')
66, điều này làm cho
>>> import os
>>> entries = os.listdir('my_directory/')
60 tạo một tệp văn bản tạm thời ở chế độ ghi. Không cần đặt tên tệp tạm thời vì nó sẽ bị hủy sau khi tập lệnh chạy xong

Sau khi ghi vào tệp, bạn có thể đọc từ tệp và đóng tệp khi xử lý xong. Sau khi đóng tệp, nó sẽ bị xóa khỏi hệ thống tệp. Nếu bạn cần đặt tên cho các tệp tạm thời được tạo bằng cách sử dụng

>>> import os
>>> entries = os.listdir('my_directory/')
60, hãy sử dụng
>>> import os
>>> entries = os.listdir('my_directory/')
69

Các tệp và thư mục tạm thời được tạo bằng cách sử dụng

>>> import os
>>> entries = os.listdir('my_directory/')
60 được lưu trữ trong một thư mục hệ thống đặc biệt để lưu trữ các tệp tạm thời. Python tìm kiếm danh sách thư mục tiêu chuẩn để tìm danh sách mà người dùng có thể tạo tệp trong đó

Trên Windows, các thư mục là

>>> import os
>>> entries = os.listdir('my_directory/')
71,
>>> import os
>>> entries = os.listdir('my_directory/')
72,
>>> import os
>>> entries = os.listdir('my_directory/')
73 và
>>> import os
>>> entries = os.listdir('my_directory/')
74, theo thứ tự đó. Trên tất cả các nền tảng khác, các thư mục là
>>> import os
>>> entries = os.listdir('my_directory/')
75,
>>> import os
>>> entries = os.listdir('my_directory/')
76 và
>>> import os
>>> entries = os.listdir('my_directory/')
77, theo thứ tự đó. Phương án cuối cùng,
>>> import os
>>> entries = os.listdir('my_directory/')
60 sẽ lưu các tệp và thư mục tạm thời trong thư mục hiện tại

>>> import os
>>> entries = os.listdir('my_directory/')
79 cũng là một trình quản lý ngữ cảnh nên có thể dùng kết hợp với câu lệnh
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
27. Sử dụng trình quản lý bối cảnh sẽ tự động đóng và xóa tệp sau khi đọc xong

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
8

Thao tác này tạo một tệp tạm thời và đọc dữ liệu từ tệp đó. Ngay sau khi nội dung của tệp được đọc, tệp tạm thời sẽ bị đóng và bị xóa khỏi hệ thống tệp

>>> import os
>>> entries = os.listdir('my_directory/')
60 cũng có thể được sử dụng để tạo các thư mục tạm thời. Hãy xem cách bạn có thể làm điều này bằng cách sử dụng
>>> import os
>>> entries = os.listdir('my_directory/')
82

>>>

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
9

Gọi

>>> import os
>>> entries = os.listdir('my_directory/')
82 tạo một thư mục tạm thời trong hệ thống tệp và trả về một đối tượng đại diện cho thư mục này. Trong ví dụ trên, thư mục được tạo bằng trình quản lý ngữ cảnh và tên của thư mục được lưu trữ trong
>>> import os
>>> entries = os.listdir('my_directory/')
84. Dòng thứ ba in ra tên của thư mục tạm thời và
>>> import os
>>> entries = os.listdir('my_directory/')
85 xác nhận xem thư mục có thực sự được tạo trong hệ thống tệp hay không

Sau khi trình quản lý ngữ cảnh ra khỏi ngữ cảnh, thư mục tạm thời sẽ bị xóa và lệnh gọi tới ____19_______85 trả về ___19_______40, điều đó có nghĩa là thư mục đã được xóa thành công

Loại bỏ các quảng cáo

Xóa tập tin và thư mục

Bạn có thể xóa các tệp, thư mục đơn lẻ và toàn bộ cây thư mục bằng cách sử dụng các phương pháp có trong các mô-đun

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
02,
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
04 và
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05. Các phần sau đây mô tả cách xóa các tệp và thư mục mà bạn không cần nữa

Xóa tệp trong Python

Để xóa một tệp, hãy sử dụng

>>> import os
>>> entries = os.listdir('my_directory/')
91,
>>> import os
>>> entries = os.listdir('my_directory/')
92. hoặc
>>> import os
>>> entries = os.listdir('my_directory/')
93

>>> import os
>>> entries = os.listdir('my_directory/')
92 và
>>> import os
>>> entries = os.listdir('my_directory/')
93 giống hệt nhau về mặt ngữ nghĩa. Để xóa một tệp bằng cách sử dụng
>>> import os
>>> entries = os.listdir('my_directory/')
92, hãy làm như sau

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

0

Xóa tệp bằng cách sử dụng

>>> import os
>>> entries = os.listdir('my_directory/')
93 tương tự như cách bạn thực hiện bằng cách sử dụng
>>> import os
>>> entries = os.listdir('my_directory/')
92

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

1

Gọi

>>> import os
>>> entries = os.listdir('my_directory/')
99 hoặc
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
00 trên một tệp sẽ xóa tệp đó khỏi hệ thống tệp. Hai hàm này sẽ ném ra một
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
56 nếu đường dẫn được truyền cho chúng trỏ đến một thư mục thay vì một tệp. Để tránh điều này, bạn có thể kiểm tra xem những gì bạn đang cố xóa có thực sự là một tệp hay không và chỉ xóa nó nếu có, hoặc bạn có thể sử dụng xử lý ngoại lệ để xử lý
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
56

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

2

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
62 kiểm tra xem
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
04 có thực sự là một tệp không. Nếu có, nó sẽ bị xóa bởi lệnh gọi tới
>>> import os
>>> entries = os.listdir('my_directory/')
92. Nếu
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
04 trỏ đến một thư mục, một thông báo lỗi sẽ được in ra bảng điều khiển

Ví dụ sau đây cho thấy cách sử dụng xử lý ngoại lệ để xử lý lỗi khi xóa tệp

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

3

Đoạn mã trên cố gắng xóa tệp trước khi kiểm tra loại tệp. Nếu

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
04 thực sự không phải là một tệp, thì ______13_______56 được ném ra sẽ được xử lý trong mệnh đề
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
09 và một thông báo lỗi được in ra bảng điều khiển. Thông báo lỗi được in ra được định dạng bằng Python f-strings

Cuối cùng, bạn cũng có thể sử dụng

>>> import os
>>> entries = os.listdir('my_directory/')
91 để xóa tệp

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

4

Điều này tạo ra một đối tượng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
31 có tên là
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
04 trỏ đến một tệp. Gọi
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
00 trên
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
04 sẽ xóa
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
15. Nếu
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
04 trỏ đến một thư mục, thì một ____21_______17 được nâng lên. Điều đáng chú ý là chương trình Python ở trên có cùng quyền với người dùng đang chạy nó. Nếu người dùng không có quyền xóa tệp, một
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
18 sẽ xuất hiện

Xóa thư mục

Thư viện chuẩn cung cấp các hàm sau để xóa thư mục

  • >>> os.listdir('my_directory/')
    ['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
    
    19
  • >>> os.listdir('my_directory/')
    ['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
    
    20
  • >>> os.listdir('my_directory/')
    ['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
    
    21

Để xóa một thư mục hoặc thư mục, hãy sử dụng

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
19 hoặc
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
23. Hai chức năng này chỉ hoạt động nếu thư mục bạn đang cố xóa trống. Nếu thư mục không trống, một
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
56 sẽ xuất hiện. Đây là cách xóa một thư mục

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

5

Ở đây, thư mục

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
25 bị xóa bằng cách chuyển đường dẫn của nó tới
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
19. Nếu thư mục không trống, một thông báo lỗi sẽ được in ra màn hình

>>>

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

6

Ngoài ra, bạn có thể sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05 để xóa thư mục

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

7

Tại đây, bạn tạo một đối tượng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
31 trỏ đến thư mục cần xóa. Gọi
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
29 trên đối tượng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
31 sẽ xóa nó nếu nó trống

Loại bỏ các quảng cáo

Xóa toàn bộ cây thư mục

Để xóa các thư mục không trống và toàn bộ cây thư mục, Python cung cấp

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
21

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

8

Mọi thứ trong

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
25 sẽ bị xóa khi
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
21 được gọi trên đó. Có thể có trường hợp bạn muốn xóa các thư mục trống theo cách đệ quy. Bạn có thể thực hiện việc này bằng cách sử dụng một trong các phương pháp đã thảo luận ở trên cùng với
>>> import os
>>> entries = os.listdir('my_directory/')
49

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

9

Điều này đi xuống cây thư mục và cố gắng xóa từng thư mục mà nó tìm thấy. Nếu thư mục không trống, một

my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
56 được nâng lên và thư mục đó bị bỏ qua. Bảng dưới đây liệt kê các chức năng được đề cập trong phần này

Chức năng Mô tả

>>> import os
>>> entries = os.listdir('my_directory/')
92Xóa một tệp và không xóa các thư mục
>>> import os
>>> entries = os.listdir('my_directory/')
93Giống với
>>> import os
>>> entries = os.listdir('my_directory/')
92 và xóa một tệp duy nhất
>>> import os
>>> entries = os.listdir('my_directory/')
91Xóa một tệp và không thể xóa các thư mục
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
19Xóa một thư mục trống
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
20Xóa một thư mục trống
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
21Xóa toàn bộ cây thư mục và có thể được sử dụng để xóa các thư mục không trống

Sao chép, di chuyển và đổi tên tệp và thư mục

Python vận chuyển với mô-đun

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
04.
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
04 là viết tắt của các tiện ích vỏ. Nó cung cấp một số thao tác cấp cao trên tệp để hỗ trợ sao chép, lưu trữ và xóa tệp và thư mục. Trong phần này, bạn sẽ học cách di chuyển và sao chép tệp và thư mục

Sao chép tệp trong Python

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
04 cung cấp một số chức năng để sao chép tệp. Các chức năng được sử dụng phổ biến nhất là
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
46 và
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
47. Để sao chép tệp từ vị trí này sang vị trí khác bằng cách sử dụng
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
46, hãy làm như sau

import os

with os.scandir('my_directory/') as entries:
    for entry in entries:
        print(entry.name)
0

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
46 có thể so sánh với lệnh
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
50 trong các hệ thống dựa trên UNIX.
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
51 sẽ sao chép tệp
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
52 đến địa điểm được chỉ định trong
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
53. Nếu
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
53 là một tệp, nội dung của tệp đó sẽ được thay thế bằng nội dung của
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
52. Nếu
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
53 là một thư mục, thì
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
52 sẽ được sao chép vào thư mục đó.
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
46 chỉ sao chép nội dung của tệp và quyền của tệp. Các siêu dữ liệu khác như thời gian tạo và sửa đổi của tệp không được lưu giữ

Để bảo toàn tất cả siêu dữ liệu của tệp khi sao chép, hãy sử dụng

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
47

import os

with os.scandir('my_directory/') as entries:
    for entry in entries:
        print(entry.name)
1

Việc sử dụng

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
60 sẽ lưu giữ các chi tiết về tệp như thời gian truy cập lần cuối, bit quyền, thời gian sửa đổi lần cuối và cờ

Sao chép thư mục

Trong khi

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
46 chỉ sao chép một tệp duy nhất, thì
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
62 sẽ sao chép toàn bộ thư mục và mọi thứ chứa trong đó.
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
63 có hai đối số. một thư mục nguồn và thư mục đích nơi các tệp và thư mục sẽ được sao chép vào

Đây là một ví dụ về cách sao chép nội dung của một thư mục sang một vị trí khác

>>>

import os

with os.scandir('my_directory/') as entries:
    for entry in entries:
        print(entry.name)
2

Trong ví dụ này,

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
64 sao chép nội dung của
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
65 đến một vị trí mới
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
66 và trả về thư mục đích. Thư mục đích không được tồn tại. Nó sẽ được tạo cũng như các thư mục mẹ bị thiếu.
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
62 là một cách hay để sao lưu tệp của bạn

Loại bỏ các quảng cáo

Di chuyển tập tin và thư mục

Để di chuyển một tệp hoặc thư mục đến một vị trí khác, hãy sử dụng

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
68

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
52 là tệp hoặc thư mục sẽ được di chuyển và
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
53 là đích đến

>>>

import os

with os.scandir('my_directory/') as entries:
    for entry in entries:
        print(entry.name)
3

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
71 di chuyển
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
72 thành
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
73 nếu tồn tại
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
73. Nếu
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
73 không tồn tại,
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
72 sẽ được đổi tên thành
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
88

Đổi tên tập tin và thư mục

Python bao gồm

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
78 để đổi tên tệp và thư mục

>>>

import os

with os.scandir('my_directory/') as entries:
    for entry in entries:
        print(entry.name)
4

Dòng trên sẽ đổi tên

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
79 thành
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
80. Nếu đường dẫn đích trỏ đến một thư mục, nó sẽ tăng giá trị
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
56

Một cách khác để đổi tên tệp hoặc thư mục là sử dụng

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
82 từ mô-đun
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05

>>>

import os

with os.scandir('my_directory/') as entries:
    for entry in entries:
        print(entry.name)
5

Để đổi tên tệp bằng cách sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
05, trước tiên bạn tạo một đối tượng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
22 chứa đường dẫn đến tệp bạn muốn thay thế. Bước tiếp theo là gọi
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
82 trên đối tượng đường dẫn và chuyển tên tệp mới cho tệp hoặc thư mục bạn đang đổi tên

lưu trữ

Lưu trữ là một cách thuận tiện để đóng gói nhiều tệp thành một. Hai loại lưu trữ phổ biến nhất là ZIP và TAR. Các chương trình Python bạn viết có thể tạo, đọc và trích xuất dữ liệu từ kho lưu trữ. Bạn sẽ học cách đọc và ghi vào cả hai định dạng lưu trữ trong phần này

Đọc tệp ZIP

Mô-đun

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
87 là mô-đun cấp thấp, là một phần của Thư viện chuẩn Python.
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
87 có các chức năng giúp dễ dàng mở và giải nén tệp ZIP. Để đọc nội dung của tệp ZIP, điều đầu tiên cần làm là tạo một đối tượng
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
89. Các đối tượng
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
89 tương tự như các đối tượng tệp được tạo bằng cách sử dụng
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
07.
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
89 cũng là một trình quản lý ngữ cảnh và do đó hỗ trợ cho câu lệnh
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
27

import os

with os.scandir('my_directory/') as entries:
    for entry in entries:
        print(entry.name)
6

Tại đây, bạn tạo một đối tượng

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
89, chuyển vào tên của tệp ZIP để mở ở chế độ đọc. Sau khi mở tệp ZIP, thông tin về kho lưu trữ có thể được truy cập thông qua các chức năng được cung cấp bởi mô-đun
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
87. Kho lưu trữ
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
96 trong ví dụ trên được tạo từ thư mục có tên
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
87 chứa tổng cộng 5 tệp và 1 thư mục con

import os

with os.scandir('my_directory/') as entries:
    for entry in entries:
        print(entry.name)
7

Để lấy danh sách các tệp trong kho lưu trữ, hãy gọi

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
98 trên đối tượng
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
89

import os

with os.scandir('my_directory/') as entries:
    for entry in entries:
        print(entry.name)
8

Điều này tạo ra một danh sách

import os

with os.scandir('my_directory/') as entries:
    for entry in entries:
        print(entry.name)
9

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
00 trả về danh sách tên của các tệp và thư mục trong kho lưu trữ. Để truy xuất thông tin về các tệp trong kho lưu trữ, hãy sử dụng
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
01

sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
0

Đây là đầu ra

sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
1

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
01 trả về một đối tượng
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
03 lưu trữ thông tin về một thành viên của kho lưu trữ. Để lấy thông tin về một tệp trong kho lưu trữ, bạn chuyển đường dẫn của nó dưới dạng đối số tới
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
01. Sử dụng
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
05, bạn có thể truy xuất thông tin về các thành viên lưu trữ, chẳng hạn như ngày tệp được sửa đổi lần cuối, kích thước nén và tên tệp đầy đủ của chúng. Truy cập
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
06 truy xuất kích thước ban đầu của tệp theo byte

Ví dụ sau đây cho thấy cách truy xuất thêm chi tiết về các tệp đã lưu trữ trong REPL của Python. Giả sử rằng mô-đun

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
87 đã được nhập và
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
08 là cùng một đối tượng mà bạn đã tạo trong các ví dụ trước

>>>

sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
2

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
08 chứa thông tin chi tiết về
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
10, chẳng hạn như kích thước của nó khi được nén và đường dẫn đầy đủ của nó

Dòng đầu tiên hiển thị cách truy xuất ngày sửa đổi cuối cùng của tệp. Dòng tiếp theo cho biết cách lấy kích thước của tệp sau khi nén. Dòng cuối cùng hiển thị đường dẫn đầy đủ của

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
10 trong kho lưu trữ

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
89 hỗ trợ giao thức quản lý ngữ cảnh, đó là lý do tại sao bạn có thể sử dụng nó với câu lệnh
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
27. Thao tác này sẽ tự động đóng đối tượng
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
89 sau khi bạn thực hiện xong. Cố gắng mở hoặc trích xuất tệp từ một đối tượng
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
89 đã đóng sẽ gây ra lỗi

Loại bỏ các quảng cáo

Giải nén kho lưu trữ ZIP

Mô-đun

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
87 cho phép bạn trích xuất một hoặc nhiều tệp từ kho lưu trữ ZIP thông qua
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
17 và
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
18

Các phương thức này trích xuất tệp vào thư mục hiện tại theo mặc định. Cả hai đều có tham số

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
19 tùy chọn cho phép bạn chỉ định một thư mục khác để giải nén tệp vào. Nếu thư mục không tồn tại, nó sẽ tự động được tạo. Để giải nén các tệp từ kho lưu trữ, hãy làm như sau

>>>

sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
3

Dòng mã thứ ba là một cuộc gọi đến

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14, cho biết thư mục hiện tại chỉ có một tệp,
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
96

Tiếp theo, bạn mở

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
96 ở chế độ đọc và gọi
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
17 để trích xuất
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
67 từ nó.
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
17 trả về đường dẫn tệp đầy đủ của tệp được giải nén. Vì không có đường dẫn cụ thể, nên
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
17 trích xuất
my_directory/
|
├── sub_dir/
|   ├── bar.py
|   └── foo.py
|
├── sub_dir_b/
|   └── file4.txt
|
├── sub_dir_c/
|   ├── config.py
|   └── file5.txt
|
├── file1.py
├── file2.csv
└── file3.txt
67 vào thư mục hiện tại

Dòng tiếp theo in danh sách thư mục cho biết thư mục hiện tại bao gồm tệp được giải nén ngoài kho lưu trữ gốc. Dòng sau đó cho biết cách giải nén toàn bộ kho lưu trữ vào thư mục

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
28.
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
18 tạo
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
30 và trích xuất nội dung của
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
96 vào đó. Dòng cuối cùng đóng kho lưu trữ ZIP

Trích xuất dữ liệu từ kho lưu trữ được bảo vệ bằng mật khẩu

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
87 hỗ trợ giải nén các tệp ZIP được bảo vệ bằng mật khẩu. Để giải nén các tệp ZIP được bảo vệ bằng mật khẩu, hãy nhập mật khẩu vào phương thức
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
17 hoặc
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
18 làm đối số

>>>

sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
4

Thao tác này sẽ mở kho lưu trữ

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
35 ở chế độ đọc. Mật khẩu được cung cấp cho
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
18 và nội dung lưu trữ được trích xuất tới
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
30. Kho lưu trữ được đóng tự động sau khi trích xuất hoàn tất nhờ câu lệnh
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
27

Tạo kho lưu trữ ZIP mới

Để tạo một kho lưu trữ ZIP mới, bạn mở một đối tượng

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
89 ở chế độ ghi (
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
09) và thêm các tệp bạn muốn lưu trữ

>>>

sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
5

Trong ví dụ,

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
41 được mở ở chế độ ghi và mỗi tệp trong
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
42 được thêm vào kho lưu trữ. Khi bộ câu lệnh
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
27 kết thúc,
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
41 được đóng lại. Mở tệp ZIP ở chế độ ghi sẽ xóa nội dung của kho lưu trữ và tạo một kho lưu trữ mới

Để thêm tệp vào kho lưu trữ hiện có, hãy mở đối tượng

>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
89 ở chế độ chắp thêm rồi thêm tệp

>>>

sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
6

Tại đây, bạn mở kho lưu trữ

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
46 mà bạn đã tạo trong ví dụ trước ở chế độ chắp thêm. Mở đối tượng
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
89 ở chế độ chắp thêm cho phép bạn thêm tệp mới vào tệp ZIP mà không xóa nội dung hiện tại của tệp. Sau khi thêm tệp vào tệp ZIP, câu lệnh
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
27 nằm ngoài ngữ cảnh và đóng tệp ZIP

Mở kho lưu trữ TAR

Tệp TAR là kho lưu trữ tệp không nén như ZIP. Chúng có thể được nén bằng các phương pháp nén gzip, bzip2 và lzma. Lớp

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
49 cho phép đọc và ghi các kho lưu trữ TAR

Làm điều này để đọc từ một kho lưu trữ

sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
7

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
50 đối tượng mở giống như hầu hết các đối tượng giống như tệp. Chúng có hàm
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
07 có chế độ xác định cách mở tệp

Sử dụng các chế độ

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
52,
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
53 hoặc
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
54 để mở tệp TAR không nén để đọc, viết và nối thêm tương ứng. Để mở các tệp TAR đã nén, hãy chuyển một đối số chế độ tới
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
55 ở dạng
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
56. Bảng bên dưới liệt kê các chế độ có thể mở tệp TAR trong

ModeAction

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
08Mở kho lưu trữ để đọc với nén trong suốt
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
58Mở kho lưu trữ để đọc với nén gzip
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
59Mở kho lưu trữ để đọc với nén bzip2
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
60Mở kho lưu trữ để đọc với nén lzma
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
09Mở kho lưu trữ để viết với nén không nén
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
62Mở kho lưu trữ để đọc với nén gzip
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
63Mở kho lưu trữ để đọc với nén lzma viết_______23____6 Mở không nén___23_______6

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
65 mặc định ở chế độ
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
52. Để đọc tệp TAR không nén và truy xuất tên của các tệp trong đó, hãy sử dụng
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
67

>>>

sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
8

Điều này trả về một danh sách có tên của nội dung lưu trữ

Ghi chú. Với mục đích chỉ cho bạn cách sử dụng các phương pháp đối tượng

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
50 khác nhau, tệp TAR trong các ví dụ được mở và đóng theo cách thủ công trong phiên REPL tương tác

Tương tác với tệp TAR theo cách này cho phép bạn xem đầu ra của việc chạy từng lệnh. Thông thường, bạn sẽ muốn sử dụng trình quản lý ngữ cảnh để mở các đối tượng giống như tệp

Siêu dữ liệu của mỗi mục trong kho lưu trữ có thể được truy cập bằng các thuộc tính đặc biệt

>>> ______36_______9

Trong ví dụ này, bạn lặp qua danh sách các tệp được trả về bởi

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
69 và in ra các thuộc tính của từng tệp. Các đối tượng được trả về bởi
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
69 có các thuộc tính có thể được truy cập theo chương trình, chẳng hạn như tên, kích thước và thời gian sửa đổi lần cuối của từng tệp trong kho lưu trữ. Sau khi đọc hoặc ghi vào kho lưu trữ, nó phải được đóng lại để giải phóng tài nguyên hệ thống

Giải nén tệp từ kho lưu trữ TAR

Trong phần này, bạn sẽ tìm hiểu cách giải nén tệp từ kho lưu trữ TAR bằng các phương pháp sau

  • >>> entries = os.listdir('my_directory/')
    >>> for entry in entries:
    ..     print(entry)
    ...
    ...
    sub_dir_c
    file1.py
    sub_dir_b
    file3.txt
    file2.csv
    sub_dir
    
    17
  • >>> entries = os.listdir('my_directory/')
    >>> for entry in entries:
    ..     print(entry)
    ...
    ...
    sub_dir_c
    file1.py
    sub_dir_b
    file3.txt
    file2.csv
    sub_dir
    
    72
  • >>> entries = os.listdir('my_directory/')
    >>> for entry in entries:
    ..     print(entry)
    ...
    ...
    sub_dir_c
    file1.py
    sub_dir_b
    file3.txt
    file2.csv
    sub_dir
    
    18

Để trích xuất một tệp từ kho lưu trữ TAR, hãy sử dụng

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
74, chuyển tên tệp

>>>

from pathlib import Path

entries = Path('my_directory/')
for entry in entries.iterdir():
    print(entry.name)
0

Tệp

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
75 được trích xuất từ ​​kho lưu trữ vào hệ thống tệp. Gọi
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
14 xác nhận rằng tệp
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
75 đã được giải nén thành công vào thư mục hiện tại. Để giải nén hoặc trích xuất mọi thứ từ kho lưu trữ, hãy sử dụng
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
18

>>>

from pathlib import Path

entries = Path('my_directory/')
for entry in entries.iterdir():
    print(entry.name)
1

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
18 có một đối số
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
19 tùy chọn để chỉ định vị trí của các tệp được giải nén. Tại đây, kho lưu trữ được giải nén vào thư mục
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
81. Các lệnh sau cho biết kho lưu trữ đã được trích xuất thành công

from pathlib import Path

entries = Path('my_directory/')
for entry in entries.iterdir():
    print(entry.name)
2

Để trích xuất một đối tượng tệp để đọc hoặc ghi, hãy sử dụng

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
72, lấy tên tệp hoặc đối tượng
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
83 để trích xuất làm đối số.
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
72 trả về một đối tượng giống như tệp có thể được đọc và sử dụng

>>>

from pathlib import Path

entries = Path('my_directory/')
for entry in entries.iterdir():
    print(entry.name)
3

Các kho lưu trữ đã mở phải luôn được đóng lại sau khi chúng đã được đọc hoặc ghi vào. Để đóng kho lưu trữ, hãy gọi

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
85 trên phần xử lý tệp lưu trữ hoặc sử dụng câu lệnh
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
27 khi tạo đối tượng
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
50 để tự động đóng kho lưu trữ khi bạn hoàn tất. Điều này giải phóng tài nguyên hệ thống và ghi bất kỳ thay đổi nào bạn đã thực hiện vào kho lưu trữ vào hệ thống tệp

Tạo kho lưu trữ TAR mới

Đây là cách bạn làm điều đó

>>>

from pathlib import Path

entries = Path('my_directory/')
for entry in entries.iterdir():
    print(entry.name)
4

Trước tiên, bạn tạo danh sách các tệp sẽ được thêm vào kho lưu trữ để bạn không phải thêm từng tệp theo cách thủ công

Dòng tiếp theo sử dụng trình quản lý ngữ cảnh

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
27 để mở một kho lưu trữ mới có tên là
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
89 ở chế độ ghi. Mở một kho lưu trữ ở chế độ ghi (
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
53) cho phép bạn ghi các tệp mới vào kho lưu trữ. Mọi tệp hiện có trong kho lưu trữ sẽ bị xóa và một kho lưu trữ mới được tạo

Sau khi kho lưu trữ được tạo và điền, trình quản lý ngữ cảnh

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
27 sẽ tự động đóng và lưu vào hệ thống tệp. Ba dòng cuối cùng mở kho lưu trữ bạn vừa tạo và in ra tên của các tệp có trong đó

Để thêm tệp mới vào kho lưu trữ hiện có, hãy mở kho lưu trữ ở chế độ chắp thêm (

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
54)

>>>

from pathlib import Path

entries = Path('my_directory/')
for entry in entries.iterdir():
    print(entry.name)
5

Mở một kho lưu trữ ở chế độ nối thêm cho phép bạn thêm các tệp mới vào đó mà không xóa các tệp đã có trong đó

Làm việc với kho lưu trữ nén

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
50 cũng có thể đọc và ghi các tệp lưu trữ TAR được nén bằng cách nén gzip, bzip2 và lzma. Để đọc hoặc ghi vào kho lưu trữ nén, hãy sử dụng
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
55, chuyển sang chế độ thích hợp cho loại nén

Ví dụ: để đọc hoặc ghi dữ liệu vào kho lưu trữ TAR được nén bằng gzip, hãy sử dụng chế độ

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
95 hoặc
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
96 tương ứng

>>>

from pathlib import Path

entries = Path('my_directory/')
for entry in entries.iterdir():
    print(entry.name)
6

Chế độ

>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
96 mở kho lưu trữ để ghi nén gzip và
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
95 mở kho lưu trữ để đọc nén gzip. Không thể mở kho lưu trữ đã nén ở chế độ chắp thêm. Để thêm tệp vào kho lưu trữ nén, bạn phải tạo một kho lưu trữ mới

Một cách dễ dàng hơn để tạo kho lưu trữ

Thư viện chuẩn Python cũng hỗ trợ tạo tệp lưu trữ TAR và ZIP bằng các phương thức cấp cao trong mô-đun

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
04. Các tiện ích lưu trữ trong
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
04 cho phép bạn tạo, đọc và trích xuất các tệp lưu trữ ZIP và TAR. Các tiện ích này dựa trên các mô-đun
>>> entries = os.listdir('my_directory/')
>>> for entry in entries:
..     print(entry)
...
...
sub_dir_c
file1.py
sub_dir_b
file3.txt
file2.csv
sub_dir
50 và
>>> os.listdir('my_directory/')
['sub_dir_c', 'file1.py', 'sub_dir_b', 'file3.txt', 'file2.csv', 'sub_dir']
87 cấp thấp hơn

Làm việc với tài liệu lưu trữ bằng cách sử dụng

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

03

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

03 có ít nhất hai đối số. tên của kho lưu trữ và định dạng lưu trữ

Theo mặc định, nó nén tất cả các tệp trong thư mục hiện tại thành định dạng lưu trữ được chỉ định trong đối số

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

05. Bạn có thể chuyển vào một đối số
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

06 tùy chọn để nén các tệp trong một thư mục khác.
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

07 hỗ trợ các định dạng lưu trữ
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

08,
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

09,
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

10 và
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

11

Đây là cách tạo kho lưu trữ TAR bằng cách sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
04

from pathlib import Path

entries = Path('my_directory/')
for entry in entries.iterdir():
    print(entry.name)
7

Điều này sao chép mọi thứ trong

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

13 và tạo một kho lưu trữ có tên là
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

14 trong hệ thống tệp và trả về tên của nó. Để trích xuất kho lưu trữ, hãy gọi
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

15

from pathlib import Path

entries = Path('my_directory/')
for entry in entries.iterdir():
    print(entry.name)
8

Gọi

>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

15 và chuyển vào tên lưu trữ và thư mục đích sẽ trích xuất nội dung của
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

14 thành
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

18. Lưu trữ ZIP có thể được tạo và trích xuất theo cách tương tự

Đọc nhiều tệp

Python hỗ trợ đọc dữ liệu từ nhiều luồng đầu vào hoặc từ danh sách tệp thông qua mô-đun

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
06. Mô-đun này cho phép bạn lặp lại nội dung của một hoặc nhiều tệp văn bản một cách nhanh chóng và dễ dàng. Đây là cách điển hình mà
with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
06 được sử dụng

from pathlib import Path

entries = Path('my_directory/')
for entry in entries.iterdir():
    print(entry.name)
9

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
06 nhận đầu vào từ các đối số dòng lệnh được chuyển đến
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

22 theo mặc định

Sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
06 để lặp qua nhiều tệp

Hãy sử dụng

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
06 để xây dựng một phiên bản thô của tiện ích UNIX chung
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

25. Tiện ích
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

25 đọc các tệp một cách tuần tự, ghi chúng vào đầu ra tiêu chuẩn. Khi được cung cấp nhiều hơn một tệp trong các đối số dòng lệnh của nó,
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

25 sẽ nối các tệp văn bản và hiển thị kết quả trong thiết bị đầu cuối

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
00

Chạy cái này trên hai tệp văn bản trong thư mục hiện tại của tôi sẽ tạo ra kết quả sau

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
01

with open('data.txt', 'w') as f:
    data = 'some data to be written to the file'
    f.write(data)
06 cho phép bạn truy xuất thêm thông tin về từng dòng chẳng hạn như đó có phải là dòng đầu tiên hay không (
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

29), số dòng (
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

30) và tên tệp (
>>> import os
>>> entries = os.scandir('my_directory/')
>>> entries

31). Bạn có thể đọc thêm về nó ở đây

Phần kết luận

Bây giờ bạn đã biết cách sử dụng Python để thực hiện các thao tác phổ biến nhất trên tệp và nhóm tệp. Bạn đã tìm hiểu về các mô-đun tích hợp khác nhau được sử dụng để đọc, tìm và thao tác với chúng

Bây giờ bạn đã được trang bị để sử dụng Python để

  • Nhận nội dung thư mục và thuộc tính tệp
  • Tạo thư mục và cây thư mục
  • Tìm các mẫu trong tên tệp
  • Tạo tập tin và thư mục tạm thời
  • Di chuyển, đổi tên, sao chép và xóa tệp hoặc thư mục
  • Đọc và trích xuất dữ liệu từ các loại tài liệu lưu trữ khác nhau
  • Đọc đồng thời nhiều tệp bằng cách sử dụng
    with open('data.txt', 'w') as f:
        data = 'some data to be written to the file'
        f.write(data)
    
    06

Đánh dấu là đã hoàn thành

Xem ngay Hướng dẫn này có một khóa học video liên quan do nhóm Real Python tạo. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn. Công thức thực tế để làm việc với tệp trong Python

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Python nhập tệp văn bản từ thư mục khác

Gửi cho tôi thủ thuật Python »

Giới thiệu về Vuyisile Ndlovu

Python nhập tệp văn bản từ thư mục khác
Python nhập tệp văn bản từ thư mục khác

Nhà phát triển Django và người đam mê mã nguồn mở

» Tìm hiểu thêm về Vuyisil


Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Python nhập tệp văn bản từ thư mục khác

David

Python nhập tệp văn bản từ thư mục khác

Geir Arne

Python nhập tệp văn bản từ thư mục khác

Joanna

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Python nhập tệp văn bản từ thư mục khác

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Chuyên gia Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng các chuyên gia Pythonistas

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi