Remove file if the new name already exists : remove file « File « Python Tutorial






import os

oldFileName = "test.txt"
newFileName = "test.old"

for file in os.listdir("c:/"):
    if file.startswith("output"):
        print file

if os.access(newFileName, os.X_OK):
    print "Removing " + newFileName
    os.remove(newFileName)

os.rename(oldFileName, newFileName)

for file in os.listdir("c:/"):
    if file.startswith("output"):
        print file








12.19.remove file
12.19.1.Remove file if the new name already exists