{"id":9560,"date":"2021-06-13T17:51:20","date_gmt":"2021-06-13T12:21:20","guid":{"rendered":"https:\/\/pynative.com\/?p=9560"},"modified":"2024-01-25T14:45:53","modified_gmt":"2024-01-25T09:15:53","slug":"python-rename-file","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-rename-file\/","title":{"rendered":"Rename Files in Python"},"content":{"rendered":"\n
In this tutorial, you will learn how to rename files and folders in Python.<\/p>\n\n\n\n
After reading this article, you’ll learn<\/strong>: –<\/p>\n\n\n\n To rename a file, please follow these steps:<\/p> To rename a file, we need its path. The path is the location of the file on the disk. Save an old name and a new name in two separate variables. Use the \n
Table of contents<\/h2>
Steps to Rename File in Python<\/h2>\n\n\n\n
An absolute path<\/strong> contains the complete directory<\/a> list required to locate the file.
A relative path<\/strong> contains the current directory and then the file name.<\/p> <\/li>old_name = 'details.txt'<\/code>
new_name = 'new_details.txt'<\/code><\/p> <\/li>
os.rename()<\/code> method to rename a file in a folder. Pass both the old and new names to the
os.rename(old_name, new_name)<\/code> function to rename a file.<\/p> <\/li><\/ol><\/div>\n<\/div>\n<\/div>\n\n\n\n
os.rename()<\/code><\/h3>\n\n\n\n