python / python/cpython

os.scandir(): add option to yield the given path itself

Đang mở
#99,454 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Feature or enhancement

I think it would be nice if os.scandir() had an option (that defaults to the current behaviour) which makes it also yield the root path itself as given by its path argument.

The benefit would be, that if one has code like e.g.:

def scandirtree(path):
    for p in os.scandir(path):
        if p.is_dir(follow_symlinks=False):
            yield p
            yield from scandirtree(p)
        else:
            yield p

one would also yield path and process that as well (in whatever one does with the generator).

It doesn't seem to be possible right now to write a warpper around scandir which would just yield path first, since one cannot create os.DirEntry objects (see #71983).

Pitch

The above in turn would be the familiar behaviour of many tools, e.g.

$ rm -rf path

also removes the path itself, not just its contents.

find path

also finds path itself.
Etc. pp.

So anyone who wants to implement a tool like these or similar behaviour, would no longer need to specially handle the path itself.

Previous discussion

https://discuss.python.org/t/os-scandir-should-have-an-option-to-also-yield-the-path-itself/18383

Thanks,
Chris.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với entry point os.scandir() và xem xét hạn chế liên quan được thảo luận trong issue #71983 và cuộc thảo luận được liên kết. Xác định một hành vi opt-in vẫn giữ nguyên mặc định hiện tại, đồng thời trả về chính đường dẫn đã cho dưới dạng một kết quả tương thích, sau đó bổ sung coverage thể hiện cả hai hành vi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
operating-systems
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
32/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.