Remove file if the new name already exists : Remove file : File PYTHON TUTORIALS


PYTHON TUTORIALS » File » Remove file »

 

Remove file if the new name already exists


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



Leave a Comment / Note


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


PYTHON TUTORIALS

 Navioo File
» Remove file