Friday, September 20, 2013

Python - Rename files in batch

I have a directory which contains a lot of photos. But, for some reasons, the files's names do not have file extension. Something like:


Because of that, I cannot upload those photos to facebook. The file-chooser window does not recognize them as images. So, I have to add .JPEG extension for all photos. Python helps me this time (and so many other times). Run this python script inside the myphotos directory;

#! /usr/bin/python

from os import listdir, rename
from os.path import isfile, join

def get_files_list(path):
return [ f for f in listdir(path) if (isfile(join(path,f)))]

def add_ext_batch(files_list):
for fn in files_list:
         tem_fn = fn + '.JPEG'
rename(fn, tem_fn)

if __name__=="__main__":
       file_list = get_files_list('.')