We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
如果该目录下是图集,可能会有上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.")
The text was updated successfully, but these errors were encountered:
项目代码中判断文件是否存在时没有遍历。
Sorry, something went wrong.
No branches or pull requests
如果该目录下是图集,可能会有上w个文件,这个时候判断起来就会很慢,如果你有 100 个文件需要判断,它将遍历目录 100 次,就会很卡硬盘io
def is_exists(path: Path) -> bool:
return path.exists()
可以考虑先读取该目录中的所有文件,然后在内存中进行判断,而不是重复查询文件系统。例如,可以使用 os.listdir() 来列出目录中的所有文件,然后再检查每个文件是否存在:
The text was updated successfully, but these errors were encountered: