def rename_v2(src, dst, overwrite=False): """Rename or move a file / directory. Args: src: string, pathname for a file dst: string, pathname to which the file needs to be moved overwrite: boolean, if false it's an error for `dst` to be occupied by an existing file. Raises: errors.OpError: If the operation fails. """ _pywrap_file_io.RenameFile(compat.path_to_bytes(src), compat.path_to_bytes(dst), overwrite)
def copy_v2(src, dst, overwrite=False): """Copies data from `src` to `dst`. Args: src: string, name of the file whose contents need to be copied dst: string, name of the file to which to copy to overwrite: boolean, if false it's an error for `dst` to be occupied by an existing file. Raises: errors.OpError: If the operation fails. """ _pywrap_file_io.CopyFile(compat.path_to_bytes(src), compat.path_to_bytes(dst), overwrite)
def list_directory_v2(path): """Returns a list of entries contained within a directory. The list is in arbitrary order. It does not contain the special entries "." and "..". Args: path: string, path to a directory Returns: [filename1, filename2, ... filenameN] as strings Raises: errors.NotFoundError if directory doesn't exist """ if not is_directory(path): raise errors.NotFoundError( node_def=None, op=None, message="Could not find directory {}".format(path)) # Convert each element to string, since the return values of the # vector of string should be interpreted as strings, not bytes. return [ compat.as_str_any(filename) for filename in _pywrap_file_io.GetChildren(compat.path_to_bytes(path)) ]
def _prewrite_check(self): if not self._writable_file: if not self._write_check_passed: raise errors.PermissionDeniedError(None, None, "File isn't open for writing") self._writable_file = _pywrap_file_io.WritableFile( compat.path_to_bytes(self.__name), compat.as_bytes(self.__mode))
def delete_recursively_v2(path): """Deletes everything under path recursively. Args: path: string, a path Raises: errors.OpError: If the operation fails. """ _pywrap_file_io.DeleteRecursively(compat.path_to_bytes(path))
def delete_file_v2(path): """Deletes the path located at 'path'. Args: path: string, a path Raises: errors.OpError: Propagates any errors reported by the FileSystem API. E.g., `NotFoundError` if the path does not exist. """ _pywrap_file_io.DeleteFile(compat.path_to_bytes(path))
def recursive_create_dir_v2(path): """Creates a directory and all parent/intermediate directories. It succeeds if path already exists and is writable. Args: path: string, name of the directory to be created Raises: errors.OpError: If the operation fails. """ _pywrap_file_io.RecursivelyCreateDir(compat.path_to_bytes(path))
def is_directory_v2(path): """Returns whether the path is a directory or not. Args: path: string, path to a potential directory Returns: True, if the path is a directory; False otherwise """ try: return _pywrap_file_io.IsDirectory(compat.path_to_bytes(path)) except errors.OpError: return False
def create_dir_v2(path): """Creates a directory with the name given by `path`. Args: path: string, name of the directory to be created Notes: The parent directories need to exist. Use `tf.io.gfile.makedirs` instead if there is the possibility that the parent dirs don't exist. Raises: errors.OpError: If the operation fails. """ _pywrap_file_io.CreateDir(compat.path_to_bytes(path))
def recursive_create_dir_v2(path): """Creates a directory and all parent/intermediate directories. It succeeds if path already exists and is writable. If RecursivelyCreateDir method does not work it tries os method. Args: path: string, name of the directory to be created Raises: errors.OpError: If the operation fails. """ try: _pywrap_file_io.RecursivelyCreateDir(compat.path_to_bytes(path)) except: if not os.path.exists(path): os.makedirs(path)
def file_exists_v2(path): """Determines whether a path exists or not. Args: path: string, a path Returns: True if the path exists, whether it's a file or a directory. False if the path does not exist and there are no filesystem errors. Raises: errors.OpError: Propagates any errors reported by the FileSystem API. """ try: _pywrap_file_io.FileExists(compat.path_to_bytes(path)) except errors.NotFoundError: return False return True
def file_exists_v2(path): """Determines whether a path exists or not. >>> with open("/tmp/x", "w") as f: ... f.write("asdf") ... 4 >>> tf.io.gfile.exists("/tmp/x") True You can also specify the URI scheme for selecting a different filesystem: >>> # for a GCS filesystem path: >>> # tf.io.gfile.exists("gs://bucket/file") >>> # for a local filesystem: >>> with open("/tmp/x", "w") as f: ... f.write("asdf") ... 4 >>> tf.io.gfile.exists("file:///tmp/x") True This currently returns `True` for existing directories but don't rely on this behavior, especially if you are using cloud filesystems (e.g., GCS, S3, Hadoop): >>> tf.io.gfile.exists("/tmp") True Args: path: string, a path Returns: True if the path exists, whether it's a file or a directory. False if the path does not exist and there are no filesystem errors. Raises: errors.OpError: Propagates any errors reported by the FileSystem API. """ try: _pywrap_file_io.FileExists(compat.path_to_bytes(path)) except errors.NotFoundError: return False return True
def has_atomic_move(path): """Checks whether the file system supports atomic moves. Returns whether or not the file system of the given path supports the atomic move operation for a file or folder. If atomic move is supported, it is recommended to use a temp location for writing and then move to the final location. Args: path: string, path to a file Returns: True, if the path is on a file system that supports atomic move False, if the file system does not support atomic move. In such cases we need to be careful about using moves. In some cases it is safer not to use temporary locations in this case. """ try: return _pywrap_file_io.HasAtomicMove(compat.path_to_bytes(path)) except errors.OpError: # defaults to True return True
def copy_v2(src, dst, overwrite=False): """Copies data from `src` to `dst`. >>> with open("/tmp/x", "w") as f: ... f.write("asdf") ... 4 >>> tf.io.gfile.exists("/tmp/x") True >>> tf.io.gfile.copy("/tmp/x", "/tmp/y") >>> tf.io.gfile.exists("/tmp/y") True >>> tf.io.gfile.remove("/tmp/y") You can also specify the URI scheme for selecting a different filesystem: >>> with open("/tmp/x", "w") as f: ... f.write("asdf") ... 4 >>> tf.io.gfile.copy("/tmp/x", "file:///tmp/y") >>> tf.io.gfile.exists("/tmp/y") True >>> tf.io.gfile.remove("/tmp/y") Note that you need to always specify a file name, even if moving into a new directory. This is because some cloud filesystems don't have the concept of a directory. >>> with open("/tmp/x", "w") as f: ... f.write("asdf") ... 4 >>> tf.io.gfile.mkdir("/tmp/new_dir") >>> tf.io.gfile.copy("/tmp/x", "/tmp/new_dir/y") >>> tf.io.gfile.exists("/tmp/new_dir/y") True >>> tf.io.gfile.rmtree("/tmp/new_dir") If you want to prevent errors if the path already exists, you can use `overwrite` argument: >>> with open("/tmp/x", "w") as f: ... f.write("asdf") ... 4 >>> tf.io.gfile.copy("/tmp/x", "file:///tmp/y") >>> tf.io.gfile.copy("/tmp/x", "file:///tmp/y", overwrite=True) >>> tf.io.gfile.remove("/tmp/y") Note that the above will still result in an error if you try to overwrite a directory with a file. Note that you cannot copy a directory, only file arguments are supported. Args: src: string, name of the file whose contents need to be copied dst: string, name of the file to which to copy to overwrite: boolean, if false it's an error for `dst` to be occupied by an existing file. Raises: errors.OpError: If the operation fails. """ _pywrap_file_io.CopyFile(compat.path_to_bytes(src), compat.path_to_bytes(dst), overwrite)