Posts

Showing posts with the label listdir

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: myphotos/ -------------photo1 -------------photo2 -------------photo3 ... -------------photo70 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('.')        add_ext_batch(file_list)