DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Rename Files

09.30.2009
| 4591 views |
  • submit to reddit
        
import os
print 'path?'
path=raw_input()
files0=[]
files1=[]
if os.path.isdir(path):
    if path[-1]!='\\':
        path=path+'\\'
    files=os.listdir(path)
    for file in files:
        if os.path.isfile(path+file):
            files0.append(file) #add old names which should be changed
            s=file+'.jpg'#how to rename
            files1.append(s) #add new names
            print file, s
    print 'Y or N?'
    s=raw_input()
    if s in ['y','Y']:
        i=0
        for file in files0:
            print file
            os.rename(path+files0[i],path+files1[i])
            i=i+1