1# Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15 16"""Import router for file_io.""" 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21# pylint: disable=unused-import 22from tensorflow.python.lib.io.file_io import copy as Copy 23from tensorflow.python.lib.io.file_io import create_dir as MkDir 24from tensorflow.python.lib.io.file_io import delete_file as Remove 25from tensorflow.python.lib.io.file_io import delete_recursively as DeleteRecursively 26from tensorflow.python.lib.io.file_io import file_exists as Exists 27from tensorflow.python.lib.io.file_io import FileIO as _FileIO 28from tensorflow.python.lib.io.file_io import get_matching_files as Glob 29from tensorflow.python.lib.io.file_io import is_directory as IsDirectory 30from tensorflow.python.lib.io.file_io import list_directory as ListDirectory 31from tensorflow.python.lib.io.file_io import recursive_create_dir as MakeDirs 32from tensorflow.python.lib.io.file_io import rename as Rename 33from tensorflow.python.lib.io.file_io import stat as Stat 34from tensorflow.python.lib.io.file_io import walk as Walk 35# pylint: enable=unused-import 36from tensorflow.python.util.deprecation import deprecated 37from tensorflow.python.util.tf_export import tf_export 38 39 40@tf_export('io.gfile.GFile', v1=['gfile.GFile', 'gfile.Open', 'io.gfile.GFile']) 41class GFile(_FileIO): 42 r"""File I/O wrappers without thread locking. 43 44 The main roles of the `tf.io.gfile` module are: 45 46 1. To provide an API that is close to Python's file I/O objects, and 47 2. To provide an implementation based on TensorFlow's C++ FileSystem API. 48 49 The C++ FileSystem API supports multiple file system implementations, 50 including local files, Google Cloud Storage (using a `gs://` prefix, and 51 HDFS (using an `hdfs://` prefix). TensorFlow exports these as `tf.io.gfile`, 52 so that you can use these implementations for saving and loading checkpoints, 53 writing to TensorBoard logs, and accessing training data (among other uses). 54 However, if all your files are local, you can use the regular Python file 55 API without any problem. 56 57 *Note*: though similar to Python's I/O implementation, there are semantic 58 differences to make `tf.io.gfile` more efficient for backing filesystems. For 59 example, a write mode file will not be opened until the first write call to 60 minimize RPC invocations in network filesystems. 61 62 Once you obtain a `GFile` object, you can use it in most ways as you would any 63 Python's file object: 64 65 >>> with open("/tmp/x", "w") as f: 66 ... f.write("asdf") 67 4 68 >>> with tf.io.gfile.GFile("/tmp/x") as f: 69 ... f.read() 70 'asdf' 71 72 The difference is that you can specify URI schemes to use other filesystems 73 (e.g., `gs://` for GCS, `s3://` for S3, etc.), if they are supported. Using 74 `file://` as an example, we have: 75 76 >>> with tf.io.gfile.GFile("file:///tmp/x", "w") as f: 77 ... f.write("qwert") 78 ... f.write("asdf") 79 >>> tf.io.gfile.GFile("file:///tmp/x").read() 80 'qwertasdf' 81 82 You can also read all lines of a file directly: 83 84 >>> with tf.io.gfile.GFile("file:///tmp/x", "w") as f: 85 ... f.write("asdf\n") 86 ... f.write("qwer\n") 87 >>> tf.io.gfile.GFile("/tmp/x").readlines() 88 ['asdf\n', 'qwer\n'] 89 90 You can iterate over the lines: 91 92 >>> with tf.io.gfile.GFile("file:///tmp/x", "w") as f: 93 ... f.write("asdf\n") 94 ... f.write("qwer\n") 95 >>> for line in tf.io.gfile.GFile("/tmp/x"): 96 ... print(line[:-1]) # removes the end of line character 97 asdf 98 qwer 99 100 Random access read is possible if the underlying filesystem supports it: 101 102 >>> with open("/tmp/x", "w") as f: 103 ... f.write("asdfqwer") 104 >>> f = tf.io.gfile.GFile("/tmp/x") 105 >>> f.read(3) 106 'asd' 107 >>> f.seek(4) 108 >>> f.tell() 109 4 110 >>> f.read(3) 111 'qwe' 112 >>> f.tell() 113 7 114 >>> f.close() 115 """ 116 117 def __init__(self, name, mode='r'): 118 super(GFile, self).__init__(name=name, mode=mode) 119 120 121@tf_export(v1=['gfile.FastGFile']) 122class FastGFile(_FileIO): 123 """File I/O wrappers without thread locking. 124 125 Note, that this is somewhat like builtin Python file I/O, but 126 there are semantic differences to make it more efficient for 127 some backing filesystems. For example, a write mode file will 128 not be opened until the first write call (to minimize RPC 129 invocations in network filesystems). 130 """ 131 132 @deprecated(None, 'Use tf.gfile.GFile.') 133 def __init__(self, name, mode='r'): 134 super(FastGFile, self).__init__(name=name, mode=mode) 135 136 137# Does not alias to Open so that we use our version of GFile to strip 138# 'b' mode. 139Open = GFile 140