Encontre Arquivos Recursivamente
Posted on jul 07, 2008 under Python | No CommentNeste post mostrarei como encontrar arquivos recursivamente com um script em Python, com todos sabem a linguagem Python é muito intuitiva para programar, ou seja, uma linguagem de alto nível.
O algoritmo utiliza uma classe em Python que usará a recursividade para listar o conteúdo de um diretório através do método def findRecursive(self,path):, onde o resultado será armazenado em uma lista, que poderá ser visualizada através do método def show(self):.
Bom chega de conversa e vamo mostrar o código.
#!/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/07/06
# Function: find files recursively 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 show(self):
for r in self.result:
print r
find = findFiles()
find.findRecursive('/home')
find.show()
Se gostaram do post assine os Feed.
