<br />

<button value="stable" default="">See Stable</button> <button value="nightly">See Nightly</button>

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/platform/gfile.py#L36-L114) |

File I/O wrappers without thread locking.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.gfile.GFile`](https://www.tensorflow.org/api_docs/python/tf/io/gfile/GFile), [`tf.compat.v1.gfile.Open`](https://www.tensorflow.org/api_docs/python/tf/io/gfile/GFile), [`tf.compat.v1.io.gfile.GFile`](https://www.tensorflow.org/api_docs/python/tf/io/gfile/GFile)

<br />

    tf.io.gfile.GFile(
        name, mode='r'
    )

### Used in the notebooks

| Used in the tutorials |
|---|
| - [Generalized Linear Models](https://www.tensorflow.org/probability/examples/Generalized_Linear_Models) - [Load LM Checkpoints using Model Garden](https://www.tensorflow.org/tfmodels/nlp/load_lm_ckpts) - [Linear Mixed-Effect Regression in {TF Probability, R, Stan}](https://www.tensorflow.org/probability/examples/HLM_TFP_R_Stan) - [Universal Sentence Encoder-Lite demo](https://www.tensorflow.org/hub/tutorials/semantic_similarity_with_tf_hub_universal_encoder_lite) - [Avro Dataset API](https://www.tensorflow.org/io/tutorials/avro) |

The main roles of the [`tf.io.gfile`](https://www.tensorflow.org/api_docs/python/tf/io/gfile) module are:

1. To provide an API that is close to Python's file I/O objects, and
2. To provide an implementation based on TensorFlow's C++ FileSystem API.

The C++ FileSystem API supports multiple file system implementations,
including local files, Google Cloud Storage (using a `gs://` prefix, and
HDFS (using an `hdfs://` prefix). TensorFlow exports these as `tf.io.gfile`,
so that you can use these implementations for saving and loading checkpoints,
writing to TensorBoard logs, and accessing training data (among other uses).
However, if all your files are local, you can use the regular Python file
API without any problem.

> [!NOTE]
> **Note:** though similar to Python's I/O implementation, there are semantic differences to make [`tf.io.gfile`](https://www.tensorflow.org/api_docs/python/tf/io/gfile) more efficient for backing filesystems. For example, a write mode file will not be opened until the first write call to minimize RPC invocations in network filesystems.

Once you obtain a `GFile` object, you can use it in most ways as you would any
Python's file object:

    with open("/tmp/x", "w") as f:
      f.write("asdf")
    4
    with tf.io.gfile.GFile("/tmp/x") as f:
      f.read()
    'asdf'

The difference is that you can specify URI schemes to use other filesystems
(e.g., `gs://` for GCS, `s3://` for S3, etc.), if they are supported. Using
`file://` as an example, we have:

    with tf.io.gfile.GFile("file:///tmp/x", "w") as f:
      f.write("qwert")
      f.write("asdf")
    tf.io.gfile.GFile("file:///tmp/x").read()
    'qwertasdf'

You can also read all lines of a file directly:

    with tf.io.gfile.GFile("file:///tmp/x", "w") as f:
      f.write("asdf\n")
      f.write("qwer\n")
    tf.io.gfile.GFile("/tmp/x").readlines()
    ['asdf\n', 'qwer\n']

You can iterate over the lines:

    with tf.io.gfile.GFile("file:///tmp/x", "w") as f:
      f.write("asdf\n")
      f.write("qwer\n")
    for line in tf.io.gfile.GFile("/tmp/x"):
      print(line[:-1]) # removes the end of line character
    asdf
    qwer

Random access read is possible if the underlying filesystem supports it:

    with open("/tmp/x", "w") as f:
      f.write("asdfqwer")
    f = tf.io.gfile.GFile("/tmp/x")
    f.read(3)
    'asd'
    f.seek(4)
    f.tell()
    4
    f.read(3)
    'qwe'
    f.tell()
    7
    f.close()

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `mode` | Returns the mode in which the file was opened. |
| `name` | Returns the file name. |

<br />

## Methods

### `close`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L223-L242)

    close()

Closes the file.

Should be called for the WritableFile to be flushed.

In general, if you use the context manager pattern, you don't need to call
this directly.

    with tf.io.gfile.GFile("/tmp/x", "w") as f:
      f.write("asdf\n")
      f.write("qwer\n")
    # implicit f.close() at the end of the block

For cloud filesystems, forgetting to call `close()` might result in data
loss as last write might not have been replicated.

### `flush`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L213-L221)

    flush()

Flushes the Writable file.

This only ensures that the data has made its way out of the process without
any guarantees on whether it's written to disk. This means that the
data would survive an application crash but not necessarily an OS crash.

### `next`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L210-L211)

    next()

### `read`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L104-L121)

    read(
        n=-1
    )

Returns the contents of a file as a string.

Starts reading from current position in file.

<br />

<br />

<br />

| Args ||
|---|---|
| `n` | Read `n` bytes if `n != -1`. If `n = -1`, reads to end of file. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `n` bytes of the file (or whole file) in bytes mode or `n` bytes of the string if in string (regular) mode. ||

<br />

### `readline`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L167-L170)

    readline()

Reads the next line, keeping \\n. At EOF, returns ''.

### `readlines`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L172-L181)

    readlines()

Returns all lines from the file in a list.

### `seek`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L123-L165)

    seek(
        offset=None, whence=0, position=None
    )

Seeks to the offset in the file. (deprecated arguments)

> [!WARNING]
> **Deprecated:** SOME ARGUMENTS ARE DEPRECATED: `(position)`. They will be removed in a future version. Instructions for updating: position is deprecated in favor of the offset argument.

<br />

<br />

<br />

| Args ||
|---|---|
| `offset` | The byte count relative to the whence argument. |
| `whence` | Valid values for whence are: 0: start of the file (default) 1: relative to the current position of the file 2: relative to the end of file. `offset` is usually negative. |

<br />

### `seekable`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L244-L246)

    seekable()

Returns True as FileIO supports random access ops of seek()/tell()

### `size`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L94-L96)

    size()

Returns the size of the file.

### `tell`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L183-L191)

    tell()

Returns the current position in the file.

### `write`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L98-L102)

    write(
        file_content
    )

Writes file_content to the file. Appends to the end of the file.

### `__enter__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L193-L195)

    __enter__()

Make usable with "with" statement.

### `__exit__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L197-L199)

    __exit__(
        unused_type, unused_value, unused_traceback
    )

Make usable with "with" statement.

### `__iter__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/lib/io/file_io.py#L201-L202)

    __iter__()