python file does not exist_python – os.path.exists()的谎言

File Handle Caching

Directories cache file names to file handles mapping. The most common problems with this are:

?You have an opened file, and you need to check if the file has been replaced by a newer file. You have to flush the parent directory’s file handle cache before stat() returns the new file’s information and not the opened file’s.

?Actually this case has another problem: The old file may have been deleted and replaced by a new file, but both of the files may have the same inode. You can check this case by flushing the open file’s attribute cache and then seeing if fstat() fails with ESTALE.

?You need to check if a file exists. For example a lock file. Kernel may have cached that the file does not exist, even if in reality it does. You have to flush the parent directory’s negative file handle cache to to see if the file really exists.

A few ways to flush the file handle cache:

?If the parent directory’s mtime changed, the file handle cache gets flushed by flushing its attribute cache. This should work quite well if the NFS server supports nanosecond or microsecond resolution.

?Linux: chown() the directory to its current owner. The file handle cache is flushed if the call returns successfully.

?Solaris 9, 10: The only way is to try to rmdir() the parent directory. ENOTEMPTY means the cache is flushed. Trying to rmdir() the current directory fails with EINVAL and doesn’t flush the cache.

?FreeBSD 6.2: The only way is to try to rmdir() either the parent directory or the file under it. ENOTEMPTY, ENOTDIR and EACCES failures mean the cache is flushed, but ENOENT did not flush it. FreeBSD does not cache negative entries, so they do not have to be flushed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将下面代码简洁化:def split_dataset(img_path, target_folder_path, output_path): filename = [] total_imgs = os.listdir(img_path) #for root, dirs, files in os.walk(img_path): for img in total_imgs: filename.append(img) np.random.shuffle(filename) train = filename[:int(len(filename) * 0.9)] test = filename[int(len(filename) * 0.9):] out_images = os.path.join(output_path, 'imgs') if not os.path.exists(out_images): os.makedirs(out_images) out_images_train = os.path.join(out_images, 'training') if not os.path.exists(out_images_train): os.makedirs(out_images_train) out_images_test = os.path.join(out_images, 'test') if not os.path.exists(out_images_test): os.makedirs(out_images_test) out_annotations = os.path.join(output_path, 'annotations') if not os.path.exists(out_annotations): os.makedirs(out_annotations) out_annotations_train = os.path.join(out_annotations, 'training') if not os.path.exists(out_annotations_train): os.makedirs(out_annotations_train) out_annotations_test = os.path.join(out_annotations, 'test') if not os.path.exists(out_annotations_test): os.makedirs(out_annotations_test) for i in train: print(os.path.join(img_path, i)) print(os.path.join(out_images_train, i)) shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_train, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_train, annotations_name)) for i in test: shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_test, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_test, annotations_name))
最新发布
07-12
def split_dataset(img_path, target_folder_path, output_path): filename = os.listdir(img_path) np.random.shuffle(filename) train = filename[:int(len(filename) * 0.9)] test = filename[int(len(filename) * 0.9):] out_images = os.path.join(output_path, 'imgs') os.makedirs(out_images, exist_ok=True) out_images_train = os.path.join(out_images, 'training') os.makedirs(out_images_train, exist_ok=True) out_images_test = os.path.join(out_images, 'test') os.makedirs(out_images_test, exist_ok=True) out_annotations = os.path.join(output_path, 'annotations') os.makedirs(out_annotations, exist_ok=True) out_annotations_train = os.path.join(out_annotations, 'training') os.makedirs(out_annotations_train, exist_ok=True) out_annotations_test = os.path.join(out_annotations, 'test') os.makedirs(out_annotations_test, exist_ok=True) for i in train: img_src = os.path.join(img_path, i) img_dst = os.path.join(out_images_train, i) shutil.copyfile(img_src, img_dst) annotations_name = "gt_" + i[:-3] + 'txt' annotations_src = os.path.join(target_folder_path, annotations_name) annotations_dst = os.path.join(out_annotations_train, annotations_name) shutil.copyfile(annotations_src, annotations_dst) for i in test: img_src = os.path.join(img_path, i) img_dst = os.path.join(out_images_test, i) shutil.copyfile(img_src, img_dst) annotations_name = "gt_" + i[:-3] + 'txt' annotations_src = os.path.join(target_folder_path, annotations_name) annotations_dst = os.path.join(out_annotations_test, annotations_name) shutil.copyfile(annotations_src, annotations_dst)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值