Pythonos.rename() Method
Example
Rename a file of the specified path:
#Import os Library
import os
# Rename file of the specified path
os.rename("C:/w3schools/test.txt","C:/w3school/demo.txt")
Try it Yourself »
Definition and Usage
The os.rename()
method is used to rename a file or a directory with a specified source and destination.
Syntax
os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
Parameter Values
Parameter | Description |
---|---|
src | Required. A path-like object representing a file system path of the source to be renamed. |
dst | Required.A path-like object representing a file system path. |
src_dir_fd | Optional. Represent a source directory. The default value of this parameter is None. If the path is absolute then dir_fd is ignored. |
dst_dir_fd | Optional. Represent a destination directory. The default value of this parameter is None. If the path is absolute then dir_fd is ignored. |
Technical Details
Return Value: | None |
---|---|
Python Version: | pre 2.6 |
Python Change Log: | 3.3- The src_dir_fd and dst_dir_fd argument. 3.6- Accepts a path-like object |