分享好友 人工智能首页 频道列表

【转载】 tensorflow gfile文件操作详解

tensorflow教程  2023-02-09 19:188390

 原文地址:

https://zhuanlan.zhihu.com/p/31536538

 

 

-------------------------------------------------------------------------------

 

 

 

 

一、gfile模块是什么

gfile模块定义在tensorflow/python/platform/gfile.py,但其源代码实现主要位于tensorflow/tensorflow/python/lib/io/file_io.py,那么gfile模块主要功能是什么呢?我们看到gfile.py里有那么一句简单的话“File I/O wrappers without thread locking.”用来描述gfile,翻译过来是"无线程锁的文件I/O操作包装器",可还是不明就里。于是,到google上搜索该模块的功能,

找到如下描述:

 

Why use tensorflow gfile? (for file I/O)

 

 

 

【转载】    tensorflow gfile文件操作详解

 

 

 

翻译过来就是(我的翻译水平还有待提高,哈哈,暂且看看吧):

  • 1、提供了一种类似于python文件操作的API;
  • 2、提供了一种操作tensorflow C++文件系统的API;
  • tensorflow c++文件操作接口支持多个文件系统实现,包括本地文件谷歌云存储(以gs://开头)和HDFS(以HDFS://开头),tensorflow封装这些接口到tf.gfile,以便我们可以使用这些接口来存储和加载检查点文件、将tensorboard log信息写到文本里以及访问训练数据(在其他用途里)。但是,如果所有文件都放在本地,那么我们直接使用python提供的常规文本操作接口也是一样效果且毫无问题的!

这应该就是Google对gfile模块的说明了!

 

 

 

 

 

 

 

二、gfile API介绍

下面将分别介绍每一个gfile API!

 

2-1)tf.gfile.Copy(oldpath, newpath, overwrite=False)

拷贝源文件并创建目标文件,无返回,其形参说明如下:

oldpath:带路径名字的拷贝源文件;

newpath:带路径名字的拷贝目标文件;

overwrite:目标文件已经存在时是否要覆盖,默认为false,如果目标文件已经存在则会报错

 

 

2-2)tf.gfile.MkDir(dirname)

创建一个目录,dirname为目录名字,无返回。

 

2-3)tf.gfile.Remove(filename)

删除文件,filename即文件名,无返回。

 

2-4)tf.gfile.DeleteRecursively(dirname)

递归删除所有目录及其文件,dirname即目录名,无返回。

 

2-5)tf.gfile.Exists(filename)

判断目录或文件是否存在,filename可为目录路径或带文件名的路径,有该目录则返回True,否则False。

 

2-6)tf.gfile.Glob(filename)

查找匹配pattern的文件并以列表的形式返回,filename可以是一个具体的文件名,也可以是包含通配符的正则表达式。

 

2-7)tf.gfile.IsDirectory(dirname)

判断所给目录是否存在,如果存在则返回True,否则返回False,dirname是目录名。

 

2-8)tf.gfile.ListDirectory(dirname)

罗列dirname目录下的所有文件并以列表形式返回,dirname必须是目录名。

 

2-9)tf.gfile.MakeDirs(dirname)

以递归方式建立父目录及其子目录,如果目录已存在且是可覆盖则会创建成功,否则报错,无返回。

 

2-10)tf.gfile.Rename(oldname, newname, overwrite=False)

重命名或移动一个文件或目录,无返回,其形参说明如下:

oldname:旧目录或旧文件;

newname:新目录或新文件;

overwrite:默认为false,如果新目录或新文件已经存在则会报错,否则重命名或移动成功。

 

 

2-11)tf.gfile.Stat(filename)

返回目录的统计数据,该函数会返回FileStatistics数据结构,以dir(tf.gfile.Stat(filename))获取返回数据的属性如下:

【转载】    tensorflow gfile文件操作详解

 

 

 

2-12)tf.gfile.Walk(top, in_order=True)

递归获取目录信息生成器,top是目录名,in_order默认为True指示顺序遍历目录,否则将无序遍历,每次生成返回如下格式信息(dirname, [subdirname, subdirname, ...], [filename, filename, ...])。

 

2-13)tf.gfile.GFile(filename, mode)

获取文本操作句柄,类似于python提供的文本操作open()函数,filename是要打开的文件名,mode是以何种方式去读写,将会返回一个文本操作句柄。

tf.gfile.Open()是该接口的同名,可任意使用其中一个!

 

2-14)tf.gfile.FastGFile(filename, mode)

该函数与tf.gfile.GFile的差别仅仅在于“无阻塞”,即该函数会无堵塞以较快的方式获取文本操作句柄

 

 

 

 

 

 

 

 

三、gfile示例

我将按照API介绍的顺序来举例说明每一个接口的使用方法!

假设在目录/home/xsr-ai/study/test里已经拥有如下文件:

【转载】    tensorflow gfile文件操作详解

 

 

示例代码如下:

import tensorflow as tf

tf.gfile.Copy('/home/xsr-ai/study/test/app.py', '/home/xsr-ai/study/test/app_new.py', True)

tf.gfile.MkDir('/home/xsr-ai/study/test/gfile')

tf.gfile.Remove('/home/xsr-ai/study/test/app.py')

tf.gfile.DeleteRecursively('/home/xsr-ai/study/test/hello')

pathexist = tf.gfile.Exists('/home/xsr-ai/study/test/')
print(pathexist)
print('\n')

glob = tf.gfile.Glob('/home/xsr-ai/study/test/app*new.py')
print(glob)
print('\n')

isdir = tf.gfile.IsDirectory('/home/xsr-ai/study/test/yes')
print(isdir)
print('\n')

lstdir = tf.gfile.ListDirectory('/home/xsr-ai/study/test/mnist')
print(lstdir)
print('\n')

tf.gfile.MakeDirs('/home/xsr-ai/study/test/lucky/boy/is/me')

tf.gfile.Rename('/home/xsr-ai/study/test/hello.jpg', '/home/xsr-ai/study/test/world.jpg', True)

statinfo = tf.gfile.Stat('/home/xsr-ai/study/test/')
print(statinfo.length)
print('\n')
                         
walkinfo = tf.gfile.Walk('/home/xsr-ai/study/test/')
for info in walkinfo:
    print(info)
print('\n')
                         
gfile_hd = tf.gfile.GFile('/home/xsr-ai/study/test/app_new.py', "r")
print(gfile_hd.readline())
print('\n')
                         
fast_gfile_hd = tf.gfile.FastGFile('/home/xsr-ai/study/test/app_new.py', "r")
print(fast_gfile_hd.readline())
                         
print('\ngfile example is end, good lucky!')

 

在jupyter notebook里执行该示例代码输出信息如下:

【转载】    tensorflow gfile文件操作详解

 

 

 

 

 

 

运行示例代码后目录/home/xsr-ai/study/test的情况如下:

【转载】    tensorflow gfile文件操作详解

 

 

通过该示例,可以看到gfile所有接口都有按照正确逻辑执行输出,Congratulation to me!

 

查看更多关于【tensorflow教程】的文章

展开全文
相关推荐
反对 0
举报 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
tensorflow2.0——LSTM,GRU(Sequential层版)
前面都是写的cell版本的GRU和LSTM,比较底层,便于理解原理。下面的Sequential版不用自定义state参数的形状,使用更简便: import tensorflow as tfimport osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'assert tf.__version__.startswith('2.')# 设置相关底层

0评论2023-02-10907

TensorFlow基础笔记(7) 图像风格化效果与性能优化进展
参考 http://hacker.duanshishi.com/?p=1693http://blog.csdn.net/hungryof/article/details/53981959http://blog.csdn.net/hungryof/article/details/61195783http://blog.csdn.net/wyl1987527/article/details/70245214https://www.ctolib.com/AdaIN-style.

0评论2023-02-09559

Tensorflow报错总结 TensorFlow文档
输入不对应报错内容:WARNING:tensorflow:Model was constructed with shape (None, 79) for input Tensor("genres:0", shape=(None, 79), dtype=float32), but it was called on an input with incompatible shape (128, 5).定义模型的输入和训练时候传入的in

0评论2023-02-09636

深度学习框架之TensorFlow的概念及安装(ubuntu下基于pip的安装,IDE为Pycharm)
2015年11月9日,Google发布人工智能系统TensorFlow并宣布开源。TensorFlow 是使用数据流图进行数值计算的开源软件库。也就是说,TensorFlow 使用图(graph)来表示计算任务。图中的节点表示数学运算,边表示运算之间用来交流的多维数组(也就是tensor,张量)

0评论2023-02-09857

TensorFlow源码分析——Tensor与Eigen tensorflow 开源
TensorFlow底层操作的数据结构是Tensor(张量),可以表示多维的数据,其实现在core/framework/tensor.h中,对于tensor的理解主要分两大块:1.Tensor的组成成分2.Tensor是如何进行数学运算的(TensorFlow本质就是处理大量训练数据集,在底层要实现深度学习常

0评论2023-02-09693

tensorflow scope的作用
  我们在使用tensorflow的时候,当你想复用一个函数的模块,调试时候回提示你变量已经出现,提示你是否重用。那我们当然是不重用的,因为每一个变量都是我们需要的。  要体现不同,就在不同的变量中使用name scope限定,那么其中的重复名字就不会出现问题

0评论2023-02-09697

TensorFlow——LinearRegression简单模型代码
代码函数详解tf.random.truncated_normal()函数tf.truncated_normal函数随机生成正态分布的数据,生成的数据是截断的正态分布,截断的标准是2倍的stddev。zip()函数zip() 函数用于将可迭代对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些

0评论2023-02-09381

tensorFlow2.1下的tf.data.Dataset.from_tensor_slices()的用法
一、总结一句话总结:将输入的张量的第一个维度看做样本的个数,沿其第一个维度将tensor切片,得到的每个切片是一个样本数据。实现了输入张量的自动切片。# from_tensor_slices 为输入张量的每一行创建一个带有单独元素的数据集ts = tf.constant([[1, 2], [3,

0评论2023-02-09789

TensorFlow基础笔记(14) 网络模型的保存与恢复_mnist数据实例
http://blog.csdn.net/huachao1001/article/details/78502910http://blog.csdn.net/u014432647/article/details/75276718https://zhuanlan.zhihu.com/p/32887066#coding:utf-8#http://blog.csdn.net/zhuiqiuk/article/details/53376283#http://blog.csdn.net/

0评论2023-02-091030

更多推荐