我想在返回文件之前检查它是否存在。所以在我的脚本中,我做了:
print("***********")
print(args.src_path)
print(os.path.isdir(args.src_path))
但终端返回:
(val_env) jovyan@jupyter-me:~/use-cases/UC_Scene_understanding/Code_Woodscape/scripts$ python semantic_map_generator.py --src_path ../data/instance_annotations/ --dst_path ../data/semantic_annotations --semantic_class_mapping configs/semantic_mapping_9_classes.json --instance_class_mapping scripts/mappers/class_names.json
***********
../data/instance_annotations/
False
Traceback (most recent call last):
File "/home/jovyan/use-cases/UC_Scene_understanding/Code_Woodscape/scripts/semantic_map_generator.py", line 69, in <module>
src_path, dst_path, semantic_classes_mapping, instance_classes_mapping = parser_arguments()
File "/home/jovyan/use-cases/_Scene_understanding/Code_Woodscape/scripts/semantic_map_generator.py", line 63, in parser_arguments
raise Exception("Error: Check if the files or dirs in arguments exit!")
Exception: Error: Check if the files or dirs in arguments exit!
然而,对我来说,这个../data/instance_annotations/
是一个目录的例子,os.path.isdir(args.src_path)
应该返回True。
此外,此目录存在:
$find
...
./scripts/semantic_map_generator.py
./scripts/polygon_generator.py
./scripts/mappers
./scripts/mappers/class_names.json
...
./scripts/box_2d_generator.py
./data
./data/semantic_annotations
./data/download.txt
考虑到此错误与此部分无关,并且如果False
文件或目录不存在,则os.path.isdir()
方法返回,此错误与该代码的后面几行有关(可能是在该代码中打开并读取文件的某个地方(,但是为了检查文件或目录的存在,我建议您尝试.exists()
方法,而不是.isdir()
方法。
import os
os path.exists("blob/blob/blobfile.txt")
参考:os.path.exists(path)
[Python doc]