Renaming Files : Rename file : File PYTHON TUTORIALS


PYTHON TUTORIALS » File » Rename 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



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo File
» Rename file