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

   2023-02-09 学习力839
核心提示: 原文地址:https://zhuanlan.zhihu.com/p/31536538  -------------------------------------------------------------------------------    一、gfile模块是什么gfile模块定义在tensorflow/python/platform/gfile.py,但其源代码实现主要位于tensorf

 原文地址:

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!

 

 
反对 0举报 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 深度学习-Tensorflow2.2-多分类{8}-多输出模型实例-20
    深度学习-Tensorflow2.2-多分类{8}-多输出模型
    import tensorflow as tffrom tensorflow import kerasimport matplotlib.pyplot as plt%matplotlib inlineimport numpy as npimport pathlibimport osimport randomos.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'import IPython.display as display
    03-08
  • TensorFlow与Flask结合识别手写体数字
    TensorFlow与Flask结合识别手写体数字
    阅读本文约“2.2分钟”TensorFlow框架        ——TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统        ——可被用于语音识别或图像识别等多项机器学习和深度学习领域        ——TensorFlow是将复杂的数据结构
    03-08
  • 在 GPU 上运行 TensorFlow 教程 (MNIST)
    在 GPU 上运行 TensorFlow 教程 (MNIST)
    介绍我想接触TensorFlow,暂时想运行教程。有GPU!这就是我的想法,但是我很难让它发挥作用,所以我将把信息留在这里。它是为谁准备的?想要在 GPU 上运行 TensorFlow 的人概述基本遵循TensorFlow官方教程它是作为初学者编写的,所以如果它顺利,它很容易!ht
    03-08
  • TensorFlow 开发者证书 PyCharm 相关环境创建与错误斗争+复习
    TensorFlow 开发者证书 PyCharm 相关环境创建与
    介绍2022 年 11 月TensorFlow 开发者证书通过了特定于测试PyCharm我遇到了一个相关的问题,所以我写了这篇文章,因为我想为将来参加考试的人提供有用的信息。 目标:即将考取TensorFlow开发者证书的人 环境:Windows 11 内容:考试过程中出现的错误及解决方法
    03-08
  • 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.')# 设置相关底层
    02-10
  • 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.
    02-09
  • 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
    02-09
  • 深度学习框架之TensorFlow的概念及安装(ubuntu
    2015年11月9日,Google发布人工智能系统TensorFlow并宣布开源。TensorFlow 是使用数据流图进行数值计算的开源软件库。也就是说,TensorFlow 使用图(graph)来表示计算任务。图中的节点表示数学运算,边表示运算之间用来交流的多维数组(也就是tensor,张量)
    02-09
  • tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数
    tensorflow中 tf.train.slice_input_producer
    tensorflow数据读取机制tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算。具体来说就是使用一个线程源源不断的将硬盘中的图片数据读入到一个内存队列中,另一个线程负责计算任务,所需数据直接从内存队列
    02-09
  • 机器学习 - pycharm, tensorflow集成篇
    机器学习 - pycharm, tensorflow集成篇
     继续上篇的pyspark集成后,我们再来看看当今热的不得了的tensorflow是如何继承进pycharm环境的参考:  http://blog.csdn.net/include1224/article/details/53452824思路其实很简单,说下要点吧1. python必须要3.5 64位版本(上一篇直接装的是64位版本的An
    02-09
点击排行