Renaming Files : rename file « File « Python Tutorial






import os

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

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

#Remove file if the new name already exists
if os.access(newFileName, os.X_OK):
    print "Removing " + newFileName
    os.remove(newFileName)

#Rename the file
os.rename(oldFileName, newFileName)

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








12.20.rename file
12.20.1.Renaming Files