Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọchow do we list all files in a directory in python.
Bàn luận
Trong bài viết này, chúng tôi sẽ đề cập đến cách chúng tôi liệt kê tất cả các tệp trong một thư mục trong Python.
Thư mục là gì? gfg
Một thư mục đôi khi cũng được gọi là thư mục là cấu trúc tổ chức đơn vị trong hệ thống tệp máy tính để lưu trữ và định vị các tệp hoặc nhiều thư mục hơn. Python hiện hỗ trợ một số API để liệt kê nội dung thư mục. Chẳng hạn, chúng ta có thể sử dụng path.iterdir, os.scandir, os.walk, path.rglob hoặc os.listdir hàm. & Nbsp;
- Thư mục đang sử dụng: GFGos.listdir[] method gets the list of all files and directories in a specified directory. By default, it is the current directory. Beyond the first level of folders, os.listdir[] does not return any files or folders.
Phương pháp 1: Mô -đun HĐH: os.listdir[path]
Parameters::
- & NBSP; Phương thức Os.ListDir [] có được danh sách tất cả các tệp và thư mục trong một thư mục được chỉ định. Theo mặc định, nó là thư mục hiện tại. Ngoài cấp độ đầu tiên của các thư mục, Os.ListDir [] không trả về bất kỳ tệp hoặc thư mục nào.
Cú pháp: Os.ListDir [Path]: returns a list of all files and directories in the specified path
Đường dẫn của thư mục Get all the list files in a Directory
Loại trả về: Trả về danh sách tất cả các tệp và thư mục trong đường dẫn được chỉ định
Output:
Ví dụ 1: Nhận tất cả các tệp danh sách trong một thư mụcTo get all the files, and no folders.
Python3
Python
print
[
"Python Program to print list the files in a directory."
]
Ví dụ 2: Để có được tất cả các tệp và không có thư mục.
print
os
3os
4]
import
os
Direc
=
input
[r
__10
print
[
[
4[
5=
[
7]
os
6=
os
8To get only .txt files.
Python3
Python
Ví dụ 2: Để có được tất cả các tệp và không có thư mục.
import
os
]
0print
]
2
Output:Output:
Direc
=
input
[r
__10OS.walk[] generates file names in a directory tree. This function returns a list of files in a tree structure. The method loops through all of the directories in a tree.
os
6=
os
8os.walk[top, topdown, onerror, followlinks]
os
6=
os
6__ It is the top directory from which you want to retrieve the names of the component files and folders.- Ví dụ 2.5: Để chỉ nhận các tệp .txt. Specifies that directories should be scanned from the top down when set to True. If this parameter is False, directories will be examined from the top down.
"Python Program to print list the files in a directory."
2"Python Program to print list the files in a directory."
4 It provides an error handler if an error is encountered"Python Program to print list the files in a directory."
5"Python Program to print list the files in a directory."
7"Python Program to print list the files in a directory."
8"Python Program to print list the files in a directory."
9: if set to True, visits folders referenced by system links& nbsp; os.walk [] tạo tên tệp trong một cây thư mục. Hàm này trả về một danh sách các tệp trong cấu trúc cây. Phương pháp lặp qua tất cả các thư mục trong một cái cây. returns the name of every file and folder within a directory and any of its subdirectories.
Python3
Output:
- Cú pháp: OS.WALK [TOP, TOPDOWN, ONEERROR, SAU NGÀY] is supported for Python 3.5 and greater.
Top: Đây là thư mục hàng đầu mà bạn muốn truy xuất tên của các tệp và thư mục thành phần.os.scandir[path = ‘.’]
TOPDOWN: Chỉ định rằng các thư mục nên được quét từ trên xuống khi được đặt thành true. Nếu tham số này là sai, các thư mục sẽ được kiểm tra từ trên xuống. returns an iterator of os.DirEntry object.
Python3
Python
Ví dụ 2: Để có được tất cả các tệp và không có thư mục.
import
os
Direc
=
input
[r
__10
os
6=
os
8
]
0print
=
4
Output:
os
6=
print
1print
2 print
3print
4 os
6__
Ví dụ 2.5: Để chỉ nhận các tệp .txt.glob module is used to retrieve files/path names matching a specified pattern.
print
2"Python Program to print list the files in a directory."
2print
4"Python Program to print list the files in a directory."
4With glob, we can use wild cards [“*, ?, [ranges]]to make path retrieval more simple and convenient.
Example:
Python3
"Python Program to print list the files in a directory."
5print
6 "Python Program to print list the files in a directory."
7"Python Program to print list the files in a directory."
8"Python Program to print list the files in a directory."
9
& nbsp; os.walk [] tạo tên tệp trong một cây thư mục. Hàm này trả về một danh sách các tệp trong cấu trúc cây. Phương pháp lặp qua tất cả các thư mục trong một cái cây.
print
[
input
2]
Cú pháp: OS.WALK [TOP, TOPDOWN, ONEERROR, SAU NGÀY]
"Python Program to print list the files in a directory."
5print
[r
3
print
[
[r
6]
Top: Đây là thư mục hàng đầu mà bạn muốn truy xuất tên của các tệp và thư mục thành phần.
"Python Program to print list the files in a directory."
5print
[r
3
print
[
os
10]
TOPDOWN: Chỉ định rằng các thư mục nên được quét từ trên xuống khi được đặt thành true. Nếu tham số này là sai, các thư mục sẽ được kiểm tra từ trên xuống.
"Python Program to print list the files in a directory."
5print
[r
3
Output:
- OnError: Nó cung cấp một trình xử lý lỗi nếu gặp lỗi & nbsp;method can be used to print filenames recursively if the recursive parameter is set to True.
Theo dõi: Nếu được đặt thành TRUE, các thư mục truy cập được tham chiếu bởi các liên kết hệ thống & nbsp;glob.iglob[pathname, *, recursive=False]
Example:
Python3
"Python Program to print list the files in a directory."
5print
6 "Python Program to print list the files in a directory."
7"Python Program to print list the files in a directory."
8"Python Program to print list the files in a directory."
9
& nbsp; os.walk [] tạo tên tệp trong một cây thư mục. Hàm này trả về một danh sách các tệp trong cấu trúc cây. Phương pháp lặp qua tất cả các thư mục trong một cái cây.
Cú pháp: OS.WALK [TOP, TOPDOWN, ONEERROR, SAU NGÀY]
Top: Đây là thư mục hàng đầu mà bạn muốn truy xuất tên của các tệp và thư mục thành phần.
"Python Program to print list the files in a directory."
5print
[
os
32]
Output: