Last active
November 5, 2015 10:05
-
-
Save rondinelisaad/d625b14a3b13727a427e to your computer and use it in GitHub Desktop.
script python para listar arquivos usando o modulo fnmatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# !/usr/bin/python | |
# -*- coding: utf-8 -*- | |
#script name: find_command.py | |
#Description: Executa o comando de shell find a fim de encontrar arquivos com uma dada extensao/string e que foi acessado por um dado periodo. | |
#Verifica a data de criacao, acesso e tamanho. | |
#Coded by: Rondineli | |
import subprocess, os, sys, os.path, time | |
#findCMD => busca por arquivos no diretorio corrente, com dado nome e data de criacao | |
findCMD = 'find . -name "*.py" -ctime +20' | |
#out => faz uso do modulo subprocess.Popen a fim de conseguir uma lista de argumento. | |
out = subprocess.Popen(findCMD,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE) | |
#Popen.communicate() => este metodo retorna uma tupla. Ler o data atraves do stdout e stderr ate o fim da execucao do comando ou arquivo | |
(stdout, stderr) = out.communicate() | |
#declara a variavel filelist para o obter o resultado do stdout | |
filelist = stdout.decode().split() | |
#x = contador | |
x = 0 | |
#Abre o arquivo no mode gravacao | |
arquivo = open("resultado_find.txt", "w") | |
#Grava no arquivo uma unica vez a linha abaixo, enquanto que grava o resultado da condicional while. O arquivo receberá | |
#o nome do arquivo, ultima modificacao, data de criacao e tamanho. | |
arquivo.write("RESULTADO DA BUSCA USANDO O FIND \n") | |
while x < len(filelist): | |
arquivo.write(""" | |
Arquivo: %s - Ultima modificacao: %s | |
Criado em: %s | |
Tamanho: %s Bytes""" % (filelist[x], time.ctime(os.path.getmtime(filelist[x])), time.ctime(os.path.getctime(filelist[x])), os.path.getsize(filelist[x]))) | |
x+=1 | |
arquivo.close() | |
# depuração | |
import pdb; pdb.set_trace() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
script name: find_command_2.py | |
#Description: Busca por arquivos que contenha uma dada string no seu nome. Verifica a data de criacao, acesso e tamanho. | |
#Coded by: Rondineli | |
import os | |
import sys | |
import fnmatch | |
import os.path | |
import time | |
from datetime import datetime | |
inDIR = '.' | |
pattern = '*.py' | |
fileList = [] | |
# Walk through directory | |
for dName, sdName, fList in os.walk(inDIR): | |
for fileName in fList: | |
if fnmatch.fnmatch(fileName, pattern): | |
fileList.append(os.path.join(dName, fileName)) | |
x=0 | |
while x < len(fileList): | |
print "Arquivo: %s - Ultima modificacao: %s" % (fileList[x], time.ctime(os.path.getmtime(fileList[x]))) | |
x+=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment