检查Python中是否存在大量目录

检查Python中是否存在大量目录,python,performance,Python,Performance,有没有技巧可以快速检查Python中是否存在10000个Windows目录?我目前将它们存储在一个列表中,我想知道如何快速进行检查。与多进程一起使用 os.access("/file/path/foo.txt", os.F_OK) # check file is exists os.F_OK # check file is readable os.R_OK # check file is wirteable os.W_OK # check file is execute o

有没有技巧可以快速检查Python中是否存在10000个Windows目录?我目前将它们存储在一个列表中,我想知道如何快速进行检查。

与多进程一起使用

os.access("/file/path/foo.txt", os.F_OK)

# check file is exists
os.F_OK
# check file is readable
os.R_OK
# check file is wirteable
os.W_OK
# check file is execute
os.X_OK
这是一个简单的测试

In [1]: import os, pathlib

In [2]: p = "/home/lpc/gitlab/config/test"

In [3]: %timeit pathlib.Path(p).exists()
5.85 µs ± 32.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [4]: %timeit os.path.exists(p)
1.03 µs ± 4.69 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [5]: %timeit os.access(p, os.F_OK)
526 ns ± 2.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [6]: def check(p):
    ...:     try:
    ...:         f = open(p)
    ...:         f.close
    ...:         return True
    ...:     except:
    ...:         pass

In [7]: %timeit check(p)
1.52 µs ± 4.41 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [8]: %timeit os.path.isdir(p)
1.05 µs ± 4.87 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

您可以遍历列表并调用
os.path.exists()
(对于文件和目录)、
os.path.isfile()
(对于文件)或
os.path.isdir()
(对于目录),以了解这些目录是否存在:

dir_list = [...]
for dir_entry in dir_list:
    if not os.path.isdir(dir_entry):
        # do something if the dir does not exist
    else:
        # do something if the dir exists
如果您只想检查路径的存在性而不检查它是否确实是一个目录,那么对于详细信息,还有一些更快的附加选项

如果简单地遍历列表不够快,您可以使用
ThreadPoolExecutor
以并行线程的方式遍历列表(将该列表的块(例如1000个目录)分配给每个工作者),但我怀疑这会加快很多速度,并且处理返回值(如果需要)会很复杂

WORKER_COUNT=10
CHUNK_SIZE=1000
def process_dir_list(dir_list):
    # implementation according to the snippet above
    (...)

future_list = []
with ThreadPoolExecutor(max_workers=WORKER_COUNT) as executor:
    for dir_index in range(0, len(dir_list), CHUNK_SIZE):
        future_list.append(executor.submit(process_dir_list, dir_list[dir_index:dir_index + 1000]))
    # wait for all futures to finish
    for current_future in future_list:
        # wait for the current future to finish
        result = future.result(timeout=0)
        # do something with the result, if desired

你有一个目录路径列表,你想检查一下,你的系统中有哪些目录,哪些没有,对吗?到目前为止,您尝试了什么?是的,我尝试了os.path.exists(),但我想知道其他人是否可以有更快的方法来解决这个问题。我建议添加一个小片段来解决这个问题-在StackOverflow上显示您迄今为止的尝试是一个很好的实践:-)我认为这并不快。有更好的方法可以在不打开和关闭每个文件的情况下检查文件是否存在…os.path.exists()确实比pathlib.os.access快一点。access是快速比较,您能添加
os.path.isdir()吗
因为这个问题特别询问目录的存在性,所以加入到混合中?感谢您添加它:-)-链接到我的答案,因为它涵盖了一个非常不同的方面(比较不同方法的性能)。添加了一个ThreadPoolExecutor中并行处理的示例,如果我希望分块灵活,因此,我不需要每次精确地分块1000个,是不是我只需对目录列表中的路径执行
[executor.submit(process\u dir\u list,dir\u list)]
不完全是这样,这只会让它在所有目录上重复多次-我将调整我的代码示例以进行配置。
range()中存在错误
-1000应该是那里的步长。现在应该是正确的。您还可以根据您的需要调整两个变量
WORKER\u COUNT
CHUNK\u SIZE
的内容(您的
dir\u list
可以比
CHUNK\u SIZE
除以
CHUNK\u SIZE
的工作人员少,完成后就可以开始下一个任务)。您是对的,只是做了一个测试。