Best Practice to Python Rename File for Beginners – Python Tutorial

By | August 27, 2019

In python, to rename a file, we can use os.rename(old_name, new_name) function. In this tutorial, we will introduce how to do and you also notice os.rename() function may fail for some reason.

 

Import library

import os

Rename a file

try:
    os.renames("file.pdf", 'file-test.pdf')
except Exception as e:
    print(e)

Then file.pdf will be renamed to file-test.pdf.

Leave a Reply

Your email address will not be published. Required fields are marked *