Python Glossary Русский

all Python 3osfunction

os.renames

os — renaming/moving

Like rename, but creates missing target dirs and prunes empty source dirs.

Syntax

os.renames(...)

After moving it prunes the emptied source directories with removedirs — which may remove directories that were meant to stay, empty. If permissions do not allow the removal, the new directories remain and the call fails. For a plain move that creates destination directories, separate makedirs and replace calls read more clearly.

Examples

import os
os.makedirs('src')
open('src/f.txt', 'w').close()
os.renames('src/f.txt', 'dst/sub/f.txt')
print(os.path.isfile('dst/sub/f.txt'))   # → True — недостающие dst/sub созданы
print(os.path.exists('src'))   # → False — опустевший исходный каталог убран

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy