Hướng dẫn how to get the list of files with specified extension in python? - cách lấy danh sách các tệp có phần mở rộng được chỉ định trong python?
Python v3.5+Phương pháp nhanh bằng cách sử dụng OS.Scandir trong một hàm đệ quy. Tìm kiếm tất cả các tệp với một tiện ích mở rộng được chỉ định trong thư mục và trình phụ phụ. Nó là nhanh, ngay cả khi tìm thấy 10.000 tệp. Show Tôi cũng đã bao gồm một chức năng để chuyển đổi đầu ra thành khung dữ liệu gấu trúc. . Readme.md ',' aborment.txt ',' main.py ']] .
& nbsp; glob.glob (pathName, *, recursive = false)Trả về một danh sách các tên đường dẫn khớp tên đường dẫn, phải là một chuỗi chứa thông số kỹ thuật đường dẫn. ‘**Có nghĩa là nó sẽ khớp với tất cả các mục được trả về bằng phương thức Os.ListDir (). Ví dụ 1: Nhận tất cả các thư mục và tệp trong root/home/project/mã
Syntax:
. Syntax:
['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']01 ['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']11 ['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']0 [‘Lược đồ_template.py,‘ sqlalchemy_models.py,]] List the files and directories present in root/home/project Python
['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']1 ['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']2
. Readme.md ',' aborment.txt ',' main.py ']]
['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']1 os 3Ví dụ 1.5: Chỉ liệt kê các tệp, bằng cách sử dụng chức năng OS.Path.isFile.List only the files, by using os.path.isfile function. Python3
['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']1 os 7os 8['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']0
['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']1 list_1 7list_1 8['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']0
['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']1 os 7os.listdir(path 8os.listdir(path 9= = 1['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']0 Đầu ra: ['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg'] Ví dụ 2: Liệt kê tất cả các thư mục con và phụ kiện phụ có mặt trong root/home/dự ánList all the subdirectories and sub-files present in root/home/project Python
['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']1 "root/home/project" 4['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']1 "root/home/project" 6Output:
Phương pháp 2: Sử dụng mô -đun `glob`Mô -đun GLOB tìm thấy tất cả các tên đường dẫn khớp với một mẫu được chỉ định theo các quy tắc được sử dụng bởi vỏ Unix. Chúng tôi sẽ sử dụng hàm glob.glob () để đạt được nhiệm vụ của chúng tôi. Ý tưởng đằng sau UNIX Shell giống như có nghĩa là chúng ta có thể cung cấp các mẫu giống như shell để tìm kiếm các tệp.glob.glob() function to achieve our task. The idea behind Unix shell-like means that we can provide Unix shell-like patterns for searching files. Syntax:
Trả về một danh sách các tên đường dẫn khớp tên đường dẫn, phải là một chuỗi chứa thông số kỹ thuật đường dẫn. ‘**Có nghĩa là nó sẽ khớp với tất cả các mục được trả về bằng phương thức Os.ListDir ().*‘ means that it will match all the items returned by similar to os.listdir() method. Ví dụ 1: Nhận tất cả các thư mục và tệp trong root/home/project/mã Get all the directories and files in root/home/project/code Python
['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']01 ['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']022 ['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']1 ['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']05 Output:
Ví dụ 2: Nhận tất cả các tệp python (.py) trong root/home/urpaness/code/code/database_models Get all the python (.py) files in root/home/project/code/database_models Python
['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']01 ['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']022 ['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']1 ['documents', 'code', 'charter.xlsx', 'timeline.jpg'] ['charter.xlsx', 'timeline.jpg']05 Output:
|