Python Tutorial

Friday, August 20, 2010

python rename a file

'''
   This code use for rename a file
      - also change the file format
      - example of exception handling
'''
import os
import sys

currentDirectory="C:/pythonFile.txt"
if os.path.exists(fileDirectory):
    try:
        newFileName="pythonInputFile.doc"
        newDirectory=fileDirectory[0:fileDirectory.rfind("/")+1]+newFileName
        print newDirectory
        os.rename(currentDirectory,newDirectory)
    except:
        print "Exception: ",str(sys.exc_info())
else:
    print "File Not found"





Output:
Check current directory folder

0 comments:

Post a Comment