Show
Hướng dẫn cách lấy danh sách tên file và thư mục trong python. Bạn sẽ học được cách lấy danh sách tên file và thư mục trong python, lấy danh sách đệ quy tên file và thư mục trong python Lấy danh sách tên file và thư mục trong python | os.listdir()Để lấy danh sách tên file và thư mục có trong thư mục được chỉ định, chúng ta dùng hàm os.listdir() tích hợp trong module os, với cú pháp sau đây: os.listdir(path) Trong đó Sau khi chạy, kết quả trả về một list chứa tên của tất cả file và thư mục con có trong thư mục được chỉ định. Lưu ý là hàm os.listdir() chỉ có thể lấy được các file và thư mục con nằm trực tiếp dưới thư mục được chỉ định, chứ không thể lấy được các file và thư mục chứa trong các thư mục con của nó. Ví dụ, bạn có một thư mục có cấu trúc như sau: data/ ├── user │ ├── dir1 │ ├── dir2 │ │ └── name.txt │ ├── pass.txt │ └── user.txt ├── client └── move.py Chúng ta sẽ lấy danh sách tên file và thư mục có trong thư mục import os Bạn có thể thấy trong kết quả tên các file và thư mục con nằm trực tiếp dưới thư mục Để lấy danh sách đệ quy tên file và thư mục trong python, hãy sử dụng hàm glob() được hướng dẫn ở phần Lấy danh sách đệ quy tên file và thư mục trong python | glob() Lấy danh sách tên file trong thư mục python | os.path.isfile()Khi chỉ muốn lấy danh sách tên file trong thư mục python, chúng ta cần kết hợp
hàm kiểm tra file có tồn tại hay không, và cách viết nội hàm List comprehension trong
python. import os Lấy danh sách tên thư mục trong python | os.path.isdir()Tương tự, nếu chỉ muốn
lấy danh sách tên thư mục trong python, chúng ta sử dụng kèm hàm kiểm tra folder có tồn tại hay không trong python. import os Lấy danh sách tên file và thư mục trong python | Path.iterdir()Chúng ta cũng có thể sử dụng phương thức iterdir() trong module pathlib để lấy danh sách tên file và thư mục trong python với cú pháp sau đây: os.listdir(path) Trong đó Cách sử dụng như ví dụ sau: import pathlib Lấy danh sách tên file và thư mục trong python | glob()Chúng ta cũng có thể sử dụng hàm glob() trong module glob() để lấy danh sách tên file và thư mục trong python với cách viết sau đây: import glob Bạn có thể gán đường dẫn thư mục vào biến và sử dụng như sau: import glob Lấy danh sách đệ quy tên file và thư mục trong python | glob()Để lấy danh sách đệ quy tên file và thư mục trong python, bao gồm cả các file và thư mục trong một thư mục và các thư mục con của nó, chúng ta sử dụng hàm glob() đệ quy như sau: import glob Bạn có thể gán đường dẫn thư mục vào biến và sử dụng như sau: import glob Cả hai đều đưa ra kết quả là danh sách đệ quy tên file và thư mục có trong thư mục ['./user\\', './user\\dir1', './user\\dir1\\kiyoshi', './user\\dir1\\user.txt', './user\\dir2', './user\\dir2\\kiyoshi', './user\\dir2\\user.txt', './user\\pass.txt', './user\\user.txt']
Tổng kết và thực hànhTrên đây Kiyoshi đã hướng dẫn bạn về cách lấy danh sách tên file và thư mục trong python rồi. Để nắm rõ nội dung bài học hơn, bạn hãy thực hành viết lại các ví dụ của ngày hôm nay nhé. Và hãy cùng tìm hiểu những kiến thức sâu hơn về python trong các bài học tiếp theo. Viết bởi Kiyoshi. Đã đăng ký bản quyền tác giả tại <a title="Bạn được tự do chia sẻ bài viết nhưng phải để lại đường link bài viết từ laptrinhcanban.com. Bạn không được sử dụng tài liệu cho mục đích thương mại. Không được phép chỉnh sửa nội dung được phát hành trên website của chúng tôi" style="color:#fff;background-color:silver" rel="license noopener" target="_blank" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">Creativecommons</a>&nbsp;và <a title="Bạn được tự do chia sẻ bài viết nhưng phải để lại đường link bài viết từ laptrinhcanban.com. Bạn không được sử dụng tài liệu cho mục đích thương mại. Không được phép chỉnh sửa nội dung được phát hành trên website của chúng tôi" style="color:#fff;background-color:silver" target="_blank" rel="noopener" href="https://www.dmca.com/Protection/Status.aspx?ID=1631afcd-7c4a-467d-8016-402c5073e5cd" class="dmca-badge">DMCA</a><script src="https://images.dmca.com/Badges/DMCABadgeHelper.min.js"></p><h3 style="font-size:15px">Bài viết liên quan</h3><ul class="popular-posts"><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/di-chuyen-file-trong-python/" title="Cách di chuyển file trong python" rel="bookmark">Cách di chuyển file trong python</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/copy-file-trong-python/" title="Cách copy file trong python" rel="bookmark">Cách copy file trong python</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/doc-file-trong-python/" title="Đọc file trong python" rel="bookmark">Đọc file trong python</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/doi-ten-thu-muc-trong-python/" title="Cách đổi tên thư mục trong python" rel="bookmark">Cách đổi tên thư mục trong python</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/lay-kich-thuoc-file-va-thu-muc-trong-python/" title="Lấy kích thước file và thư mục trong python" rel="bookmark">Lấy kích thước file và thư mục trong python</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/cach-doi-ten-file-trong-python/" title="Đổi tên file trong Python" rel="bookmark">Đổi tên file trong Python</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/lay-danh-sach-ten-file-va-thu-muc-theo-dieu-kien-trong-python/" title="Lấy danh sách tên file và thư mục theo điều kiện trong python" rel="bookmark">Lấy danh sách tên file và thư mục theo điều kiện trong python</a></h3></div></li><li class="popular-posts-item"><div class="popular-posts-title"><h3><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/lay-thong-tin-file-trong-python/" title="Cách lấy thông tin file trong python" rel="bookmark">Cách lấy thông tin file trong python</a></h3></div></li></ul></div><div id="keugoi"><span>Hãy chia sẻ và cùng lan tỏa kiến thức lập trình Nhật Bản tại Việt Nam!</span></div><section class="icon-bar-2" style="border-bottom:dotted grey .3px !important"><a target="_blank" title="chia sẻ qua facebook" rel="noopener noreferrer" href="https://www.facebook.com/sharer/sharer.php?u=https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/lay-danh-sach-ten-file-va-thu-muc-trong-python/" class="facebook"><i class="fa fa-facebook"></i></a> <a target="_blank" rel="noopener noreferrer" href="https://twitter.com/intent/tweet?url=https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/lay-danh-sach-ten-file-va-thu-muc-trong-python/" title="chia sẻ qua twitter" class="twitter"><i class="fa fa-twitter"></i></a> <a target="_blank" rel="noopener noreferrer" href="https://getpocket.com/edit?url=https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/lay-danh-sach-ten-file-va-thu-muc-trong-python/" title="chia sẻ qua pocket" class="pocket"><i class="fa fa-get-pocket"></i></a> <span class="zalo-share-button" title="chia sẻ qua zalo" style="margin-left:18px;margin-bottom:-3px" data-href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/lay-danh-sach-ten-file-va-thu-muc-trong-python/" data-oaid="579745863508352884" data-layout="3" data-color="blue" data-customize="false"></span></section><p class="ico-folder"><a target="_blank" href="https://laptrinhcanban.com/"><span itemprop="HOME">HOME</span></a><span class="sya"></span>>> <a target="_blank" class="article-category-link" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/">python cơ bản - lập trình python cho người mới bắt đầu</a>>><a target="_blank" class="article-category-link" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/">16. xử lý file trong python</a></p><nav id="article-nav"><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/kiem-tra-ton-tai-cua-file-thu-muc-trong-python/" id="article-nav-older" class="article-nav-link-wrap" style="float:left;text-align:right;padding-right:20px"><strong class="article-nav-caption">Bài sau</strong><div class="article-nav-title">Kiểm tra tồn tại của file và thư mục trong python</div></a><a target="_blank" href="https://laptrinhcanban.com/python/nhap-mon-lap-trinh-python/xu-ly-file-trong-python/lay-danh-sach-ten-file-va-thu-muc-theo-dieu-kien-trong-python/" id="article-nav-newer" class="article-nav-link-wrap" style="float:right;text-align:left;padding-left:20px"><strong class="article-nav-caption">Bài tiếp</strong><div class="article-nav-title">Lấy danh sách tên file và thư mục theo điều kiện trong python</div></a><span id="ezoic-pub-ad-placeholder-611" class="ezoic-adpicker-ad"></span></nav><div id="recent_posts_down"><div class="widget_athemes_tabs"><ul id="widget-tab" class="clearfix widget-tab-nav"><li class="active">Bài viết mới nhất</li></ul><div class="widget"><ul><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/tach-chuoi-trong-php/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban45.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/tach-chuoi-trong-php/">Tách chuỗi trong PHP (explode, preg_split)</a></span> <span>tháng 9 17, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/cat-chuoi-trong-php/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban21.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/cat-chuoi-trong-php/">Cắt chuỗi trong PHP (substr, mb_substr)</a></span> <span>tháng 9 17, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/tach-chuoi-thanh-mang-trong-php/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban57.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/tach-chuoi-thanh-mang-trong-php/">Tách chuỗi thành mảng trong PHP (explode)</a></span> <span>tháng 9 17, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/tim-kiem-chuoi-trong-php/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban43.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/tim-kiem-chuoi-trong-php/">Tìm kiếm chuỗi trong PHP (strpos)</a></span> <span>tháng 9 17, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/dem-so-lan-xuat-hien-cua-ky-tu-trong-chuoi-php/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban52.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/dem-so-lan-xuat-hien-cua-ky-tu-trong-chuoi-php/">Đếm số lần xuất hiện của ký tự và chuỗi trong chuỗi PHP (substr_count)</a></span> <span>tháng 9 17, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/chuyen-chuoi-thanh-mang-trong-php/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban47.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/chuyen-chuoi-thanh-mang-trong-php/">Chuyển chuỗi thành mảng trong PHP (str_split, mb_str_split)</a></span> <span>tháng 9 17, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/tach-tung-ky-tu-trong-chuoi-php/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban47.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/tach-tung-ky-tu-trong-chuoi-php/">Tách từng ký tự trong chuỗi PHP (substr, preg_split)</a></span> <span>tháng 9 17, 2022</span></div></li><li class="clearfix"><div class="widget-entry-thumbnail"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/xoa-ky-tu-trong-chuoi-php/"><img width="60px" height="60px" class="ezlazyload attachment-thumb-small size-thumb-small wp-post-image" alt="" ezimgfmt="rs rscb2 src ng ngcb2" data-ezsrc="/css/images/thumbnail_img/laptrinhcanban15.webp"></a></div><div class="widget-entry-summary"><span style="margin:0"><a target="_blank" href="https://laptrinhcanban.com/php/nhap-mon-lap-trinh-php/chuoi-trong-php/xoa-ky-tu-trong-chuoi-php/">Xóa ký tự trong chuỗi PHP</a></span> <span>tháng 9 15, 2022</span></div></li></ul></div></div></div><footer class="entry-meta entry-footer"></footer><hr class="entry-footer-hr"></div><div id="profiletitle2">Profile</div><div id="profileblock2"><div id="profilephoto2"><a target="_blank" rel="noopener" href="https://www.facebook.com/mr.nchita"><img width="100" height="100" alt="きよしです!笑" ezimgfmt="rs rscb2 src ng ngcb2" class="ezlazyload" data-ezsrc="/css/images/kiyoshi.webp"></a></div><div id="profiletext2"><p>Tác giả : <a href="https://www.facebook.com/mr.nchita" target="_blank" rel="nofollow noopener">Kiyoshi (Chis Thanh)</a></p><p>Kiyoshi là một <a href="https://www.youtube.com/c/ChisThanh" target="_blank" rel="nofollow noopener">cựu du học sinh tại Nhật Bản</a>. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.<span id="ezoic-pub-ad-placeholder-196" class="ezoic-adpicker-ad"></span></p></div></div></div></article><aside id="sidebar"><div id="sidebar_first"><div class="search" style="margin-top:-30px"><div id="fb-root"></div><script async defer crossorigin="anonymous" src="https://connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v12.0&appId=3084959888222500&autoLogAppEvents=1" nonce="KPkvxeh4">
|