Bài đăng này sẽ thảo luận về cách liệt kê tất cả các thư mục con trong một thư mục trong Python.
1. Sử dụng hàm os.listdir[]
Một giải pháp đơn giản để liệt kê tất cả các thư mục con trong một thư mục là sử dụng hàm os.listdir[]
. Tuy nhiên, điều này trả về danh sách tất cả các tệp và thư mục con trong thư mục gốc. Bạn có thể lọc danh sách được trả về bằng hàm os.path.isdir[]
để chỉ liệt kê các thư mục con.
Nhập khẩuos rootdir='path/to/dir'='path/to/dir' forfileinos.listdir[rootdir]:file inos.listdir[rootdir]: d=os.path.join[rootdir,file]d=os.path.join[rootdir,file] ifos.path.isdir[d]:ifos.path.isdir[d]: print[d]print[d] |
Tải xuống mã
& nbsp; Bạn cũng có thể dễ dàng mở rộng giải pháp để tìm kiếm trong các thư mục con, như được hiển thị bên dưới:
You can easily extend the solution to search within subdirectories as well, as shown below:
Nhập khẩuos deflistdirs[rootdir]:listdirs[rootdir]: forfileinos.listdir[rootdir]:forfileinos.listdir[rootdir]: d=os.path.join[rootdir,file]d=os.path.join[rootdir, file] ifos.path.isdir[d]:if os.path.isdir[d]: print[d]print[d] listdirs[d]listdirs[d] rootdir='path/to/dir' ='path/to/dir' listdirs[rootdir][rootdir] |
Tải xuống mã
& nbsp; Bạn cũng có thể dễ dàng mở rộng giải pháp để tìm kiếm trong các thư mục con, như được hiển thị bên dưới:
2. Sử dụng hàm os.scandir[]
Nhập khẩuos rootdir='path/to/dir'='path/to/dir' Tải xuống mãit inos.scandir[rootdir]: ifit.is_dir[]:ifit.is_dir[]: print[it.path]print[it.path] |
Tải xuống mã
& nbsp; Bạn cũng có thể dễ dàng mở rộng giải pháp để tìm kiếm trong các thư mục con, như được hiển thị bên dưới:
You can easily make the above code recursive to enable search within subdirectories:
Nhập khẩuos deflistdirs[rootdir]:listdirs[rootdir]: Tải xuống mãforit inos.scandir[rootdir]: ifit.is_dir[]:ifit.is_dir[]: print[it.path]print[it.path] listdirs[it]listdirs[it] rootdir='path/to/dir'='path/to/dir' listdirs[rootdir][rootdir] |
Tải xuống mã
& nbsp; Bạn cũng có thể dễ dàng mở rộng giải pháp để tìm kiếm trong các thư mục con, như được hiển thị bên dưới:
2. Sử dụng hàm os.scandir[]
Với Python 3.5, bạn có thể sử dụng chức năng rootdir='path/to/dir'='path/to/dir' Forit inos.Scandir [rootDir]:path in Path[rootdir].iterdir[]: ifpath.is_dir[]:if path.is_dir[]: print[path]print[path] |
Tải xuống mã
& nbsp; Bạn cũng có thể dễ dàng mở rộng giải pháp để tìm kiếm trong các thư mục con, như được hiển thị bên dưới:
Here’s the recursive version, which also searches within the subdirectories:
Với Python 3.5, bạn có thể sử dụng chức năng deflistdirs[rootdir]: listdirs[rootdir]: Forit inos.Scandir [rootDir]:forpath in Path[rootdir].iterdir[]: ifpath.is_dir[]:ifpath.is_dir[]: print[path]print[path] listdirs[path]listdirs[path] rootdir='path/to/dir'='path/to/dir' listdirs[rootdir][rootdir] |
Tải xuống mã
& nbsp; Bạn cũng có thể dễ dàng mở rộng giải pháp để tìm kiếm trong các thư mục con, như được hiển thị bên dưới:
2. Sử dụng hàm os.scandir[]
Nhập khẩuos rootdir='path/to/dir'='path/to/dir' Tải xuống mã rootdir,dirs,files in os.walk[rootdir]: & nbsp; Bạn cũng có thể dễ dàng mở rộng giải pháp để tìm kiếm trong các thư mục con, như được hiển thị bên dưới:forsubdir indirs: print[os.path.join[rootdir,subdir]]print[os.path.join[rootdir,subdir]] |
Tải xuống mã
& nbsp; Bạn cũng có thể dễ dàng mở rộng giải pháp để tìm kiếm trong các thư mục con, như được hiển thị bên dưới:
2. Sử dụng hàm os.scandir[]
Với Python 3.5, bạn có thể sử dụng chức năng rootdir='path/to/dir'='path/to/dir' Forit inos.Scandir [rootDir]:path inglob.glob[f'{rootdir}/*/']: print[path]print[path] |
Tải xuống mã
& nbsp; Bạn cũng có thể dễ dàng mở rộng giải pháp để tìm kiếm trong các thư mục con, như được hiển thị bên dưới:
Python 3.5 extended support for recursive globs using os.path.isdir[]
0 to search subdirectories and symbolic links to directories.
Với Python 3.5, bạn có thể sử dụng chức năng rootdir='path/to/dir'='path/to/dir' Forit inos.Scandir [rootDir]:path inglob.glob[f'{rootdir}/*/**/', recursive=True]: print[path]print[path] |
Tải xuống mã
& nbsp; Bạn cũng có thể dễ dàng mở rộng giải pháp để tìm kiếm trong các thư mục con, như được hiển thị bên dưới: