Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

存在文件的判断方法改进 #347

Open
sansi98h opened this issue Dec 8, 2024 · 1 comment
Open

存在文件的判断方法改进 #347

sansi98h opened this issue Dec 8, 2024 · 1 comment
Labels
需要补充(Incomplete) 需要更多问题说明

Comments

@sansi98h
Copy link

sansi98h commented Dec 8, 2024

如果该目录下是图集,可能会有上w个文件,这个时候判断起来就会很慢,如果你有 100 个文件需要判断,它将遍历目录 100 次,就会很卡硬盘io

def is_exists(path: Path) -> bool:
return path.exists()

可以考虑先读取该目录中的所有文件,然后在内存中进行判断,而不是重复查询文件系统。例如,可以使用 os.listdir() 来列出目录中的所有文件,然后再检查每个文件是否存在:

import os
from pathlib import Path

directory_path = "your_directory_path"
files_to_check = ["file1.txt", "file2.txt", "file3.txt"]  # 示例文件名

# 列出目录中的所有文件
existing_files = set(os.listdir(directory_path))

# 检查文件是否存在
for file in files_to_check:
    if file in existing_files:
        print(f"{file} exists.")
    else:
        print(f"{file} does not exist.")
@JoeanAmier
Copy link
Owner

项目代码中判断文件是否存在时没有遍历。

@JoeanAmier JoeanAmier added the 需要补充(Incomplete) 需要更多问题说明 label Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
需要补充(Incomplete) 需要更多问题说明
Projects
None yet
Development

No branches or pull requests

2 participants