牛骨文教育服务平台(让学习变的简单)
博文笔记

python里面判断文件夹或者文件的路径是否存在

创建时间:2018-01-23 投稿人: 浏览次数:512

第一种方法、os这个库里的os.path.exists()函数提供了这种用法,具体用法如下

import os
import time


time_start = time.time()
file_path = "/home/hjxu/PycharmProjects/01_cats_vs_dogs/111.jpg"
if os.path.exists(file_path):
    print "yes"
else:
    print("" file_path ""+ file_path + "" Not found"")
time_end = time.time()
print (time_end - time_start)  #"0.00031876"
第二种方法、tensorflow也包含了这个功能

主要是tensorflow.python.platform 里面的gfile函数,废话不多说,直接上代码

import time
from tensorflow.python.platform import gfile
if not gfile.Exists(file_path):  #判断文件和文件夹是否存在
    print("Image directory "" + file_path + "" not found.")
time_end = time.time()
print (time_end - time_start)  #0.0003309249


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。