Skip to content

Allow overwrites during rename. This is required for doing atomic puts. #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions hdfs/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,19 +924,22 @@ def delete(self, hdfs_path, recursive=False, skip_trash=True):
_logger.info('%r moved to trash at %r.', hdfs_path, dst_path)
return True

def rename(self, hdfs_src_path, hdfs_dst_path):
def rename(self, hdfs_src_path, hdfs_dst_path, overwrite=False):
"""Move a file or folder.

:param hdfs_src_path: Source path.
:param hdfs_dst_path: Destination path. If the path already exists and is
a directory, the source will be moved into it. If the path exists and is
a file, or if a parent destination directory is missing, this method will
raise an :class:`HdfsError`.
:param overwrite: overwrite the hdfs_dst_path if it already exists, rather
than raising an :class:`HdfsError`.

"""
_logger.info('Renaming %r to %r.', hdfs_src_path, hdfs_dst_path)
hdfs_dst_path = self.resolve(hdfs_dst_path)
res = self._rename(hdfs_src_path, destination=hdfs_dst_path)
kwargs = {"renameoptions": "overwrite"} if overwrite else {}
res = self._rename(hdfs_src_path, destination=hdfs_dst_path, **kwargs)
if not res.json()['boolean']:
raise HdfsError(
'Unable to rename %r to %r.',
Expand Down
Loading