Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục

Bạn cần lọc các thư mục; os.listdir() liệt kê tất cả các tên trong một đường dẫn nhất định. Bạn có thể sử dụng os.path.isdir() cho điều này:

basepath = '/path/to/directory'
for fname in os.listdir(basepath):
    path = os.path.join(basepath, fname)
    if os.path.isdir(path):
        # skip directories
        continue

Lưu ý rằng điều này chỉ lọc ra các thư mục sau khi theo dõi Symlinks. fname không nhất thiết là một tệp thông thường, nó cũng có thể là một liên kết với một tệp. Nếu bạn cũng cần lọc các liên kết symlink, bạn cần sử dụng not os.path.islink() trước.

Trên phiên bản Python hiện đại (3.5 hoặc mới hơn), một tùy chọn thậm chí còn tốt hơn là sử dụng hàm os.scandir(); Điều này tạo ra các trường hợp DirEntry(). Trong trường hợp phổ biến, điều này nhanh hơn vì Direntry được tải đã được lưu trữ đủ thông tin để xác định xem một mục nhập có phải là thư mục hay không:

basepath = '/path/to/directory'
for entry in os.scandir(basepath):
    if entry.is_dir():
        # skip directories
        continue
    # use entry.path to get the full path of this entry, or use
    # entry.name for the base filename

Bạn có thể sử dụng entry.is_file(follow_symlinks=False) nếu chỉ cần các tệp thông thường (và không liên kết symlink).

os.walk() làm công việc tương tự dưới mui xe; Trừ khi bạn cần tái phát các thư mục con, bạn không cần phải sử dụng os.walk() ở đây.

1 Câu trả lời cho câu hỏi này.

Bạn cần lọc các thư mục; os.listdir () liệt kê tất cả các tên trong một đường dẫn nhất định. Bạn có thể sử dụng Os.Path.isdir () cho điều này:

BASEPATH = '/PATH/TO/SỰ KHÁC BIỆT' cho fname trong Os.ListDir (BasePath): & nbsp; & nbsp; & nbsp; & nbsp; path = os.path.join (basePath, fname) & nbsp; .path.isdir (đường dẫn): & nbsp; & nbsp; & nbsp; & nbsp;
for fname in os.listdir(basepath):
    path = os.path.join(basepath, fname)
    if os.path.isdir(path):
        # skip directories
        continue

& nbsp; os.walk () thực hiện cùng một công việc dưới mui xe; Trừ khi bạn cần tái phát các thư mục con, bạn không cần phải sử dụng os.walk () tại đây.

Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
Đã trả lời ngày 6 tháng 3 năm 2019by SDEB • & NBSP; 13.300 điểm Mar 6, 2019 by SDeb
• 13,300 points

Các câu hỏi liên quan trong Python

  • Tất cả danh mục
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Apache Kafka (84)(84)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Apache Spark (596)(596)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Azure (131)(131)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Dữ liệu lớn Hadoop (1.907)(1,907)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Blockchain (1.673)(1,673)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    C# (124)(124)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    C ++ (268)(268)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Tư vấn nghề nghiệp (1.060)(1,060)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Điện toán đám mây (3,356)(3,356)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    An ninh mạng & hack đạo đức (145)(145)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Phân tích dữ liệu (1.266)(1,266)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Cơ sở dữ liệu (853)(853)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Khoa học dữ liệu (75)(75)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    DevOps & Agile (3.500)(3,500)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Tiếp thị kỹ thuật số (111)(111)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Các chủ đề về sự kiện & xu hướng (28)(28)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    IoT (Internet of Things) (387)(387)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Java (1.178)(1,178)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Kotlin (3)(3)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Quản trị Linux (384)(384)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Học máy (337)(337)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    MicroStrargety (6)(6)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    PMP (423)(423)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Power BI (516)(516)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Python (3.154)(3,154)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    RPA (650)(650)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Salesforce (92)(92)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Selenium (1.569)(1,569)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Kiểm tra phần mềm (56)(56)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Tableau (608)(608)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Talend (73)(73)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Các loại (124)(124)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Phát triển web (2.999)(2,999)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Hỏi chúng tôi bất cứ điều gì! (66)(66)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Những người khác (1.134)(1,134)
  • Hướng dẫn python os listdir exclude directories - python os listdir loại trừ thư mục
    Phát triển di động (46)(46)

Đăng ký nhận bản tin của chúng tôi và nhận được các khuyến nghị cá nhân hóa.

Bạn co săn san để tạo một tai khoản? Đăng nhập.

Làm thế nào để bạn loại trừ một thư mục trong Python?

Bạn có thể loại trừ một thư mục bằng cách nhấp chuột phải vào nó và chọn Thư mục Mark là → được loại trừ.right-clicking on it and selecting Mark Directory as → Excluded.

Hệ điều hành có bao gồm thư mục không?

Phương thức listDIR () chỉ liệt kê tất cả các tệp và thư mục cấp cao nhất bên trong một thư mục..

Hệ điều hành listddir () trong python là gì?

Hệ điều hành.Phương thức listDIR () trong Python có các tệp và thư mục có mặt trong một đường dẫn nhất định.gets the files and directories present in a given path.

Làm thế nào để tôi chỉ nhận được một tệp trong một thư mục trong Python?

Đây là các bước ...
Nhập mô -đun hệ điều hành.Mô-đun này giúp chúng tôi làm việc với chức năng phụ thuộc hệ điều hành trong Python.....
Sử dụng hàm Os.ListDir ().Hệ điều hành.....
Lặp lại kết quả.Sử dụng cho vòng lặp để lặp lại các tệp được trả về bởi hàm listDIR ().....
Sử dụng hàm isfile ().Trong mỗi lần lặp vòng lặp, sử dụng hệ điều hành ..