all Python 3osfunction
os.rename
os — renaming/moving
Rename or move a file/directory.
Syntax
os.rename(src, dst)
Behaviour with an existing destination is platform-dependent: Windows always raises FileExistsError, Unix silently replaces a file. For portable replacement there is os.replace. A move across filesystems may fail — use shutil.move for that. On POSIX a successful rename is atomic, so "write to a temp file, then rename" is the standard way to update a file without a half-written state.
Examples
import os
open('old.txt', 'w').close()
os.rename('old.txt', 'new.txt') # переименование в том же каталоге
print(os.path.exists('new.txt')) # → True
os.mkdir('folder_a')
os.rename('folder_a', 'folder_b') # каталоги переименовываются тем же вызовом
print(os.path.isdir('folder_b')) # → True
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy