Python Rename File and Directory using os.rename()
Python Rename File
Python rename() file is a technique used to rename a document or an index in Python programming. The Python rename() record technique can be proclaimed by passing two contentions named src (Source) and dst (Destination).
This is the syntax for os.rename() method
src: Source is the name of the file or directory. It should must already exist.
dst: Destination is the new name of the file or directory you want to change.
import os os.rename('bigdata-world','bigdata-world')
Let’s look at example in detail
You can rename the original file, we have changed the file name from “bigdata-world” to “bigdata-world.”
- To rename “bigdata-world” file, we going to use “rename function” in the OS module
- So when the code is executed, you can observe that a new file “bigdata-world“r. is created on the right side of the panel, which we renamed for our original file.
Here is the complete code
import os import shutil from os import path def main(): # make a duplicate of an existing file if path.exists("bigdata-world"): # get the path to the file in the current directory src = path.realpath("bigdata-world"); # rename the original file os.rename('bigdata-world','career.bigdata-world') if __name__ == "__main__": main()