Você já precisou renomear vários arquivos ?
Hoje tive essa necessidade pois baixei as fotos da máquina fotográfica com o programa Digikam, pois minha máquina baixa as fotos através do protocolo PTP. A biblioteca libghoto2 se encarrega de implementar o protocolo e o Digikam é um front end.
Bom mas o post é pra mostrar um script em Python para renomear a extensão de vários arquivos ao mesmo tempo. Quando baixei a fotos para o note a extensão ficou em Maiúsculo *.JPG foi necessário renomear para .jpg pois tem programas que não reconhecem a extensão .JPG como foto.
Esse script é uma adpatação do post Encontre Arquivos Recursivamente
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# author: Marlon Petry
# Date: 2008/09/01
# Function: Rename Files in Python
#
import os
import re
class findFiles:
def __init__(self):
self.result = []
def findRecursive(self,path):
if os.path.isdir(path) and not os.path.islink(path):
files = os.listdir(path)
self.result.append(’d ‘ + path)
for file in files:
a = os.path.join(path,file)
self.findRecursive(a)
else:
self.result.append(’- ‘ + path)
def rename(self,path,orig,new):
reg = re.compile(orig)
if os.path.isdir(path) and not os.path.islink(path):
files = os.listdir(path)
for file in files:
newExt = re.compile(orig).match
if newExt(file):
c = os.path.splitext(file)
b = c[0] + new
a = os.path.join(path,file)
b = os.path.join(path,b)
os.rename(a,b)
find = findFiles()
find.rename(’/home/marlon/fotos/demoto.blogrs.com.br/’,’.*JPG’,’.jpg’)
Espera que seja útil para mais alguém pois pra mim foi uma mão na roda. Renomear 400 arquivos na mão é muito enfadonho.
Abraços.