Mega Code Archive

 
Categories / Python Tutorial / File
 

Renaming Files

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