Monday, August 23, 2010

Rename multiple file simultaneously

Renaming multiple file  once is really little confusing using command-line. There are lots of way to do it via programming, but yet, i didnt fine any on-the-spot command to do it.
So i used python to do it simply.
My requirement was actually:
1) i have one dedicated folder, where i have to rename all files.
2) all filename to be renamed are structured, i mean, i have to rename all dedupe_<number>.csv to <number>.csv

I did this using following code,

import os
from os import listdir, getcwd, rename


list_files = listdir(getcwd())
for filename in list_files:
    if not filename.startswith('.') and 'dedupe_' in filename:
        ext = filename.split('.')[-1]
        new_name = ''.join(filename.split('.')[:-1]).replace('dedupe_','')+'.'+ext
        cmd = 'mv '+filename + ' ' +new_name
        os.popen(cmd)

isn't it very very simple !!!!

No comments: