Python scandir so với toàn cầu

Mô-đun hệ điều hành trong Python cung cấp các chức năng để tương tác với hệ điều hành. Hệ điều hành đi kèm với các mô-đun tiện ích tiêu chuẩn của Python. Mô-đun này cung cấp một cách di động để sử dụng chức năng phụ thuộc vào hệ điều hành

hệ điều hành. phương thức scandir() trong Python được sử dụng để lấy một trình vòng lặp của os. Các đối tượng DirEntry tương ứng với các mục trong thư mục được cung cấp bởi đường dẫn đã chỉ định.  

Bạn biết đấy, đôi khi tôi đọc những câu cách ngôn về trăn như "đẹp hơn xấu" tôi tự hỏi không biết các nhà sản xuất đang mỉa mai hay thật, và tôi không đùa

Nó không chỉ là về listdirscandir, rất nhiều thứ mơ hồ và bạn sẽ tìm thấy rất nhiều cách khác nhau để làm những điều tương tự, do đó góp phần tạo nên một hệ thống làm việc không theo tiêu chuẩn và lộn xộn. setuptools và distutils là những ví dụ rõ ràng, có rất nhiều thành phần setuptools vẫn sử dụng nội bộ các gói distutils cũ khiến nó trở nên đáng kinh ngạc. easy_install là một cách khác để thực hiện điệu nhảy đóng gói tương tự

Đôi khi, một chức năng trong Shutil bị hỏng và bạn cần nhập giải pháp thay thế từ các distutil cũ tốt, điều này thực sự vô lý và phi lý. Gần đây nhất, tôi tình cờ thấy điều này

from distutils.dir_util import copy_tree

cửa chớp. copy_tree bị hỏng (nó không tự động tạo các thư mục không tồn tại và có một số bất thường khác), vì vậy tôi phải sử dụng copy_tree từ distutils. mô-đun dir_util

Đây là một ví dụ tuyệt vời khác. distutils.core, pkg_resources và có một gói khác gọi là site về cơ bản tất cả đều làm một và giống nhau. Trong trường hợp này, tôi phải viết một phiên bản cục bộ của pip --freeze để kiểm tra thiết lập cục bộ của tôi. py và tạo ra các yêu cầu. txt từ đó thay vì các mô-đun được cài đặt trên toàn cầu. Như bạn có thể thấy, có lẽ có rất nhiều cách khó hiểu để thực hiện cùng một điều này

Được tạo vào 2015-11-10 11. 18 bởi xdegaye, thay đổi lần cuối 2022-04-11 14. 58 bởi quản trị viên. vấn đề giờ đã kết thúc

- (view)Tác giả. Xavier de Gaye (xdegaye) *
Python scandir so với toàn cầu
Ngày. 2015-11-10 11. 18
The glob module happily joins names of regular files together with os.path.join() or attempts to list the files contained into a regular file (sic). The same 'except os.error' statement is used to handle both these cases and the case of a non readable directory.

The attached patch makes the code more correct and easier to understand.
- (xem)Tác giả. Xavier de Gaye (xdegaye) *
Python scandir so với toàn cầu
Ngày. 2015-11-10 15. 35_______2_______ - (xem)Tác giả. Serhiy Storchaka (serhiy. storchaka) *
Python scandir so với toàn cầu
Ngày. 2016-01-02 15. 22____3_______ - (xem)Tác giả. r. David Murray (r. david. murray) *
Python scandir so với toàn cầu
Ngày. 2016-01-02 19. 49
I don't think it is a good idea to break backward compatibility here even if the globN are internal functions.  Better would be to continue to have globN functions that support the existing API and emit deprecation warnings.
- (xem)Tác giả. Serhiy Storchaka (serhiy. storchaka) *
Python scandir so với toàn cầu
Ngày. 2016-01-03 08. 23_______5_______ - (xem)Tác giả. Xavier de Gaye (xdegaye) *
Python scandir so với toàn cầu
Ngày. 2016-01-03 14. 55_______6_______ - (xem)Tác giả. Xavier de Gaye (xdegaye) *
Python scandir so với toàn cầu
Ngày. 2016-01-03 16. 30
I may be missing something, anyway here are few comments on the patch:
Is the call to entry.is_symlink() in _iterdir() necessary since is_dir() follows symlinks ?
If _iterdir() would yield the DirEntry instance instead of DirEntry.name, then _rlistdir() could use x.is_dir() to know whether it is correct to iterate over _rlistdir(x.path, dironly)
- (xem)Tác giả. Serhiy Storchaka (serhiy. storchaka) *
Python scandir so với toàn cầu
Ngày. 2016-01-03 17. 20____8_______ - (xem)Tác giả. Xavier de Gaye (xdegaye) *
Python scandir so với toàn cầu
Ngày. 2016-01-04 11. 20____9_______ - (xem)Tác giả. STINNER Victor (vstinner) *
Python scandir so với toàn cầu
Ngày. 2016-01-06 22. 59
Related issue: #26032 "Use scandir() to speed up pathlib globbing".
- (xem)Tác giả. Guido van Rossum (gvanrossum) *
Python scandir so với toàn cầu
Ngày. 2016-01-06 23. 08
This second patch rewrites the conditionals that decide whether to call _iglob() recursively, in a more natural way and without changing the behavior from the first patch.
Note that when 'dirname == pathname', then basename is empty and glob2() or glob1() are not called.

The patch also refactors the code with a new _listdir() function.
0 - (view)Tác giả. Xavier de Gaye (xdegaye) *
Python scandir so với toàn cầu
Ngày. 2016-01-08 13. 53
This second patch rewrites the conditionals that decide whether to call _iglob() recursively, in a more natural way and without changing the behavior from the first patch.
Note that when 'dirname == pathname', then basename is empty and glob2() or glob1() are not called.

The patch also refactors the code with a new _listdir() function.
1 - (xem)Tác giả. Xavier de Gaye (xdegaye) *
Python scandir so với toàn cầu
Ngày. 2016-01-10 14. 53
This second patch rewrites the conditionals that decide whether to call _iglob() recursively, in a more natural way and without changing the behavior from the first patch.
Note that when 'dirname == pathname', then basename is empty and glob2() or glob1() are not called.

The patch also refactors the code with a new _listdir() function.
2 - (xem)Tác giả. Xavier de Gaye (xdegaye) *
Python scandir so với toàn cầu
Ngày. 2016-01-10 20. 04
This second patch rewrites the conditionals that decide whether to call _iglob() recursively, in a more natural way and without changing the behavior from the first patch.
Note that when 'dirname == pathname', then basename is empty and glob2() or glob1() are not called.

The patch also refactors the code with a new _listdir() function.
3 - (xem)Tác giả. Xavier de Gaye (xdegaye) *
Python scandir so với toàn cầu
Ngày. 2016-01-10 21. 09
This second patch rewrites the conditionals that decide whether to call _iglob() recursively, in a more natural way and without changing the behavior from the first patch.
Note that when 'dirname == pathname', then basename is empty and glob2() or glob1() are not called.

The patch also refactors the code with a new _listdir() function.
4 - (view)Tác giả. Guido van Rossum (gvanrossum) *
Python scandir so với toàn cầu
Ngày. 2016-01-11 03. 14
This second patch rewrites the conditionals that decide whether to call _iglob() recursively, in a more natural way and without changing the behavior from the first patch.
Note that when 'dirname == pathname', then basename is empty and glob2() or glob1() are not called.

The patch also refactors the code with a new _listdir() function.
5 - (xem)Tác giả. Serhiy Storchaka (serhiy. storchaka) *
Python scandir so với toàn cầu
Ngày. 2016-01-11 08. 33
This second patch rewrites the conditionals that decide whether to call _iglob() recursively, in a more natural way and without changing the behavior from the first patch.
Note that when 'dirname == pathname', then basename is empty and glob2() or glob1() are not called.

The patch also refactors the code with a new _listdir() function.
6 - (xem)Tác giả. Serhiy Storchaka (serhiy. storchaka) *
Python scandir so với toàn cầu
Ngày. 2016-01-11 12. 18
This second patch rewrites the conditionals that decide whether to call _iglob() recursively, in a more natural way and without changing the behavior from the first patch.
Note that when 'dirname == pathname', then basename is empty and glob2() or glob1() are not called.

The patch also refactors the code with a new _listdir() function.
7 - (xem)Tác giả. Ben Hoyt (benhoyt) *Ngày. 2016-01-11 14. 25
This second patch rewrites the conditionals that decide whether to call _iglob() recursively, in a more natural way and without changing the behavior from the first patch.
Note that when 'dirname == pathname', then basename is empty and glob2() or glob1() are not called.

The patch also refactors the code with a new _listdir() function.
8 - (xem)Tác giả. Guido van Rossum (gvanrossum) *
Python scandir so với toàn cầu
Ngày. 2016-01-11 20. 41
This second patch rewrites the conditionals that decide whether to call _iglob() recursively, in a more natural way and without changing the behavior from the first patch.
Note that when 'dirname == pathname', then basename is empty and glob2() or glob1() are not called.

The patch also refactors the code with a new _listdir() function.
9 - (xem)Tác giả. Serhiy Storchaka (serhiy. storchaka) *
Python scandir so với toàn cầu
Ngày. 2016-01-11 20. 56
In general the patch LGTM. It allows to speed up glob by 5-10% in warn test. But much more gain we can achieve by using os.scandir(). Here are results of microbenchmarks:

$ ./python -m timeit -s "from glob import glob" -- "glob('**/*', recursive=True)"
Unpatched:        201 msec per loop
Using isdir():    181 msec per loop
Using scandir():  65.2 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/*', recursive=True)"
Unpatched:        2.06 sec per loop
Using isdir():    1.92 sec per loop
Using scandir():  820 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/', recursive=True)"
Unpatched:        1.77 sec per loop
Using isdir():    1.61 sec per loop
Using scandir():  431 msec per loop

Yet one benefit is that iglob() now yields path names without the delay for reading the full content of a directory (see issue22167).
0 - (view)Tác giả. Guido van Rossum (gvanrossum) *
Python scandir so với toàn cầu
Ngày. 2016-01-11 20. 58
In general the patch LGTM. It allows to speed up glob by 5-10% in warn test. But much more gain we can achieve by using os.scandir(). Here are results of microbenchmarks:

$ ./python -m timeit -s "from glob import glob" -- "glob('**/*', recursive=True)"
Unpatched:        201 msec per loop
Using isdir():    181 msec per loop
Using scandir():  65.2 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/*', recursive=True)"
Unpatched:        2.06 sec per loop
Using isdir():    1.92 sec per loop
Using scandir():  820 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/', recursive=True)"
Unpatched:        1.77 sec per loop
Using isdir():    1.61 sec per loop
Using scandir():  431 msec per loop

Yet one benefit is that iglob() now yields path names without the delay for reading the full content of a directory (see issue22167).
1 - (xem)Tác giả. Serhiy Storchaka (serhiy. storchaka) *
Python scandir so với toàn cầu
Ngày. 2016-02-11 11. 57
In general the patch LGTM. It allows to speed up glob by 5-10% in warn test. But much more gain we can achieve by using os.scandir(). Here are results of microbenchmarks:

$ ./python -m timeit -s "from glob import glob" -- "glob('**/*', recursive=True)"
Unpatched:        201 msec per loop
Using isdir():    181 msec per loop
Using scandir():  65.2 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/*', recursive=True)"
Unpatched:        2.06 sec per loop
Using isdir():    1.92 sec per loop
Using scandir():  820 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/', recursive=True)"
Unpatched:        1.77 sec per loop
Using isdir():    1.61 sec per loop
Using scandir():  431 msec per loop

Yet one benefit is that iglob() now yields path names without the delay for reading the full content of a directory (see issue22167).
2 - (xem)Tác giả. Dima Tisnek (Dima. Tisnek) *Ngày. 2016-08-30 12. 18
In general the patch LGTM. It allows to speed up glob by 5-10% in warn test. But much more gain we can achieve by using os.scandir(). Here are results of microbenchmarks:

$ ./python -m timeit -s "from glob import glob" -- "glob('**/*', recursive=True)"
Unpatched:        201 msec per loop
Using isdir():    181 msec per loop
Using scandir():  65.2 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/*', recursive=True)"
Unpatched:        2.06 sec per loop
Using isdir():    1.92 sec per loop
Using scandir():  820 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/', recursive=True)"
Unpatched:        1.77 sec per loop
Using isdir():    1.61 sec per loop
Using scandir():  431 msec per loop

Yet one benefit is that iglob() now yields path names without the delay for reading the full content of a directory (see issue22167).
3 - (xem)Tác giả. Serhiy Storchaka (serhiy. storchaka) *
Python scandir so với toàn cầu
Ngày. 2016-09-06 19. 33
In general the patch LGTM. It allows to speed up glob by 5-10% in warn test. But much more gain we can achieve by using os.scandir(). Here are results of microbenchmarks:

$ ./python -m timeit -s "from glob import glob" -- "glob('**/*', recursive=True)"
Unpatched:        201 msec per loop
Using isdir():    181 msec per loop
Using scandir():  65.2 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/*', recursive=True)"
Unpatched:        2.06 sec per loop
Using isdir():    1.92 sec per loop
Using scandir():  820 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/', recursive=True)"
Unpatched:        1.77 sec per loop
Using isdir():    1.61 sec per loop
Using scandir():  431 msec per loop

Yet one benefit is that iglob() now yields path names without the delay for reading the full content of a directory (see issue22167).
4 - (xem)Tác giả. Robot Roundup (python-dev)
Python scandir so với toàn cầu
Ngày. 2016-09-06 19. 35______3_______5 - (xem)Tác giả. Guido van Rossum (gvanrossum) *
Python scandir so với toàn cầu
Ngày. 2016-09-07 01. 26
In general the patch LGTM. It allows to speed up glob by 5-10% in warn test. But much more gain we can achieve by using os.scandir(). Here are results of microbenchmarks:

$ ./python -m timeit -s "from glob import glob" -- "glob('**/*', recursive=True)"
Unpatched:        201 msec per loop
Using isdir():    181 msec per loop
Using scandir():  65.2 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/*', recursive=True)"
Unpatched:        2.06 sec per loop
Using isdir():    1.92 sec per loop
Using scandir():  820 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/', recursive=True)"
Unpatched:        1.77 sec per loop
Using isdir():    1.61 sec per loop
Using scandir():  431 msec per loop

Yet one benefit is that iglob() now yields path names without the delay for reading the full content of a directory (see issue22167).
6 - (xem)Tác giả. Robot Roundup (python-dev)
Python scandir so với toàn cầu
Ngày. 2016-09-07 06. 50
In general the patch LGTM. It allows to speed up glob by 5-10% in warn test. But much more gain we can achieve by using os.scandir(). Here are results of microbenchmarks:

$ ./python -m timeit -s "from glob import glob" -- "glob('**/*', recursive=True)"
Unpatched:        201 msec per loop
Using isdir():    181 msec per loop
Using scandir():  65.2 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/*', recursive=True)"
Unpatched:        2.06 sec per loop
Using isdir():    1.92 sec per loop
Using scandir():  820 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/', recursive=True)"
Unpatched:        1.77 sec per loop
Using isdir():    1.61 sec per loop
Using scandir():  431 msec per loop

Yet one benefit is that iglob() now yields path names without the delay for reading the full content of a directory (see issue22167).
7 - (xem)Tác giả. Dima Tisnek (Dima. Tisnek) *Ngày. 2016-09-07 09. 09
In general the patch LGTM. It allows to speed up glob by 5-10% in warn test. But much more gain we can achieve by using os.scandir(). Here are results of microbenchmarks:

$ ./python -m timeit -s "from glob import glob" -- "glob('**/*', recursive=True)"
Unpatched:        201 msec per loop
Using isdir():    181 msec per loop
Using scandir():  65.2 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/*', recursive=True)"
Unpatched:        2.06 sec per loop
Using isdir():    1.92 sec per loop
Using scandir():  820 msec per loop

$ ./python -m timeit -s "from glob import glob" -- "glob('/usr/lib*/**/', recursive=True)"
Unpatched:        1.77 sec per loop
Using isdir():    1.61 sec per loop
Using scandir():  431 msec per loop

Yet one benefit is that iglob() now yields path names without the delay for reading the full content of a directory (see issue22167).
8 - (view)Tác giả. Serhiy Storchaka (serhiy. storchaka) *
Python scandir so với toàn cầu
Ngày. 2016-09-07 11. 10____3_______9