Skip to main content

file_move_safe

Move a file from one location to another in the safest way possible.

First, try os.rename, which is simple but will break across filesystems. If that fails, stream manually from one file to another in pure Python.

If the destination file exists and allow_overwrite is False, raise FileExistsError.

def file_move_safe(
old_file_name: string,
new_file_name: string,
chunk_size: integer = 65536,
allow_overwrite: boolean = False
) - > null

Move a file from one location to another in the safest way possible. First, try os.rename, which is simple but will break across filesystems. If that fails, stream manually from one file to another in pure Python.

Parameters

NameTypeDescription
old_file_namestringThe absolute or relative path to the source file that needs to be moved.
new_file_namestringThe absolute or relative path for the destination where the file should be relocated.
chunk_sizeinteger = 65536The number of bytes to read and write at a time if a manual stream is required for cross-filesystem moves.
allow_overwriteboolean = FalseDetermines whether to replace an existing file at the destination path; if False, a FileExistsError is raised if the destination exists.

Returns

TypeDescription
nullReturns None upon successful completion of the file move or if the source and destination are the same file.