diff --git a/tensorflow/python/platform/default/_gfile.py b/tensorflow/python/platform/default/_gfile.py index ac05f5cf545..74c99b651ce 100644 --- a/tensorflow/python/platform/default/_gfile.py +++ b/tensorflow/python/platform/default/_gfile.py @@ -438,3 +438,16 @@ def Copy(oldpath, newpath, overwrite=False): if not overwrite and Exists(newpath): raise OSError(errno.EEXIST, os.strerror(errno.EEXIST), newpath) shutil.copy(oldpath, newpath) + + +def Open(name, mode='r'): + """Exact API match to the standard open. + + Args: + name: a file name, either local or a gfile compatible. + mode: for example "w" to open the file for writing. + + Returns: + A threadsafe gfile.GFile object. + """ + return GFile(name, mode=mode) diff --git a/tensorflow/python/platform/default/gfile_test.py b/tensorflow/python/platform/default/gfile_test.py index 71c72907692..cb6ed42429c 100644 --- a/tensorflow/python/platform/default/gfile_test.py +++ b/tensorflow/python/platform/default/gfile_test.py @@ -220,6 +220,12 @@ class FunctionTests(_BaseTest, googletest.TestCase): lambda: gfile.Rename(self.tmp + "dir1/file1", self.tmp + "newdir/file1")) + def testOpen(self): + with gfile.Open(self.tmp + "test_open", "wb") as f: + f.write("foo") + with gfile.Open(self.tmp + "test_open") as f: + result = f.readlines() + self.assertEqual([b"foo"], result) if __name__ == "__main__": googletest.main()