目录
和Python内置模块os非常像,一般都能用os模块代替。
tf.gfile.Copy(
oldpath,
newpath,
overwrite=False
)
参数:
overwrite
:布尔值,如果为false,则newpath将被现有文件占用。可能产生的异常:
errors.OpError
: If the operation fails.tf.gfile.DeleteRecursively(dirname)
参数:
可能产生的异常:
errors.OpError
: If the operation fails.tf.gfile.Exists(filename)
参数:
返回值:
可能产生的异常:
errors.OpError
: Propagates any errors reported by the FileSystem API.1、__init__
__init__(
name,
mode='r'
)
模式
返回文件打开时的模式。
名称
返回文件名称
3、__enter__
__enter__()
使用with语句。
4、__exit__
__exit__(
unused_type,
unused_value,
unused_traceback
)
使用with语句。
5、__iter__
__iter__()
6、close
close()
FileIO关门。应被调用以刷新可写文件。
7、flush
flush()
刷新可写文件。这只能确保数据在没有任何关于是否写入磁盘的保证的情况下离开进程。这意味着数据可以在应用程序崩溃时存活,但不一定能在操作系统崩溃时存活。
8、next
next()
9、read
read(n=-1)
以字符串的形式返回文件的内容。从文件中的当前位置开始读取。
参数:
返回值:
10、readline
readline()
从文件中读取下一行。在末尾留下“\n”。
11、readlines
readlines()
返回列表中文件的所有行。
12、seek
seek(
offset=None,
whence=0,
position=None
)
查找文件中的偏移量。(弃用参数)
参数:
offset
:相对于where参数的字节数。13、seekable
seekable()
返回True,因为FileIO支持seek()/tell()的随机访问操作
14、size
size()
返回文件的大小。
15、tell
tell()
返回文件中的当前位置。
16、write
write(file_content)
将file_content写入文件。附加到文件末尾。
查找匹配pattern的文件并以列表的形式返回,filename可以是一个具体的文件名,也可以是包含通配符的正则表达式。
tf.gfile.Glob(filename)
参数:
filename
:字符串或字符串的可迭代。通配符匹配操作符(s)的模式。返回值:
可能产生的异常:
errors.OpError
: If there are filesystem / directory listing errors.返回路径是否为目录。
tf.gfile.IsDirectory(dirname)
参数:
返回值:
tf.gfile.ListDirectory(dirname)
这个列表的顺序是任意的。它不包含特殊条目“.”和“..”。
参数:
返回值:
tf.gfile.MakeDirs(dirname)
如果dirname已经存在且可写,则成功。
参数:
可能产生的异常:
errors.OpError
: If the operation fails.tf.gfile.MkDir(dirname)
参数:
可能产生的异常:
errors.OpError
: If the operation fails.tf.gfile.Remove(filename)
参数:
可能产生的异常:
errors.OpError
: Propagates any errors reported by the FileSystem API. E.g., NotFoundError if the file does not exist.tf.gfile.Rename(
oldname,
newname,
overwrite=False
)
参数:
overwrite
:布尔值,如果为false,则newname被现有文件占用是一个错误可能产生的异常:
errors.OpError
: If the operation fails.tf.gfile.Stat(filename)
参数:
文件名:字符串,文件的路径
返回值:
可能产生的异常:
errors.OpError
: If the operation fails.tf.gfile.Walk(
top,
in_order=True
)
参数:
产生:
例:
tf.gfile.FastGFile(path,decodestyle)
函数功能:实现对图片的读取。
函数参数:
(1)、path:图片所在路径。
(2)、decodestyle:图片的解码方式。(‘r’:UTF-8编码; ‘rb’:非UTF-8编码)
import matplotlib.pyplot as plt
import tensorflow as tf
#tf.gfileGFile()函数:读取图像
image_jpg = tf.gfile.FastGFile('dog.jpg','rb').read()
image_png = tf.gfile.FastGFile('lizard.png','rb').read()
with tf.Session() as sess:
image_jpg = tf.image.decode_jpeg(image_jpg) #图像解码
print(sess.run(image_jpg))#打印解码后的图像(即为一个三维矩阵[w,h,3])
image_jpg = tf.image.convert_image_dtype(image_jpg,dtype=tf.uint8) #改变图像数据类型
image_png = tf.image.decode_png(image_png)
print(sess.run(image_jpg))
image_png = tf.image.convert_image_dtype(image_png,dtype=tf.uint8)
plt.figure(1) #图像显示
plt.imshow(image_jpg.eval())
plt.figure(2)
plt.imshow(image_png.eval())