The two functions os.path.isfile(/path/src) and os.path.isdir(/path/src) can be used to check whether a source is a directory or a file.
import os
name = "test.txt"
nameNew = "new-name.txt"
try:
os.rename(name, nameNew)
if (os.path.isdir(name)):
print("Directory renamed successfully")
else:
print("File renamed successfully")
except FileExistsError:
print("Renaming cannot be done")
File renamed successfully