tensorflow-tf.gfile() file manipulation method

Introduction

These functions are very similar to the os module in python. Generally, you can use the os module instead.

One, what is the gfile module

  • The gfile module is defined in tensorflow/python/platform/gfile.py,
  • But its source code implementation is mainly located in tensorflow/tensorflow/python/lib/io/file_io.py, so what is the main function of the gfile module?

Two, gfile API introduction

The following will introduce each gfile API separately!

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

Copy the source file and create the target file, no return, its formal parameters are described as follows:

oldpath: copy source file with path name;

newpath: copy target file with path name;

overwrite: Whether to overwrite when the target file already exists, the default is false, if the target file already exists, an error will be reported

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

Create a directory, dirname is the name of the directory, no return.

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

Delete the file, filename is the file name, no return.

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

Recursively delete all directories and their files, dirname is the name of the directory, no return.

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

Judge whether a directory or file exists. filename can be a directory path or a path with a file name. If there is a directory, it will return True, otherwise it will be False.

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

Find files matching the pattern and return them in the form of a list. filename can be a specific file name or a regular expression containing wildcard characters.

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

Judge whether the given directory exists, if it exists, return True, otherwise return False, dirname is the name of the directory.

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

List all files in the dirname directory and return them in the form of a list. dirname must be a directory name.

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

Create the parent directory and its subdirectories recursively. If the directory already exists and can be overwritten, it will be created successfully, otherwise an error will be reported and no return will be made.

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

Rename or move a file or directory, no return, its formal parameter description is as follows:
oldname: old directory or old file;

newname: new directory or new file;

overwrite: The default is false. If the new directory or file already exists, an error will be reported, otherwise the rename or move is successful.

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

Returns the statistical data of the directory. This function returns the FileStatistics data structure. The attributes of the returned data are obtained by dir(tf.gfile.Stat(filename)) as follows:
Insert picture description here

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

Recursively obtain the directory information generator, top is the directory name, in_order defaults to True to indicate that the directory is traversed in order, otherwise it will be traversed out of order, and the following format information is returned each time (dirname, [subdirname, subdirname, …], [filename, filename, …]).

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

Obtaining a text operation handle, similar to the text operation open() function provided by python, filename is the name of the file to be opened, and the mode in which to read and write, will return a text operation handle.

  • tf.gfile.Open() is the same name of the interface, you can use any of them!

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

The difference between this function and tf.gfile.GFile is only "non-blocking", that is, this function will get the text operation handle in a faster way without any hindrance.

Three Reference

https://blog.csdn.net/a373595475/article/details/79693430

Guess you like

Origin blog.csdn.net/m0_38024592/article/details/112536672