Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87903

Re: Regex Python Help

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'string.': 0.05; 'subject:Python': 0.06; 'correct.': 0.07; 'modify': 0.07; 'sys': 0.07; 'advance': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'spaces': 0.09; 'subject:Help': 0.11; 'jan': 0.12; '"r")': 0.16; 'file))': 0.16; 'filesystem': 0.16; 'mixture': 0.16; 'os.walk': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'tabs': 0.16; 'wrote:': 0.18; 'help.': 0.21; 'import': 0.22; 'print': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'file.': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'appear': 0.29; 'code': 0.31; '100000': 0.31; 'directory,': 0.31; 'file': 0.32; 'open': 0.33; 'could': 0.34; 'problem': 0.35; 'tool': 0.35; 'but': 0.35; 'skip:o 20': 0.38; 'thank': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'pm,': 0.38; 'bad': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'simple': 0.61; 'email addr:gmail.com': 0.63; 'name': 0.63; 'close': 0.67; 'line,': 0.68; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Regex Python Help
Date Tue, 24 Mar 2015 15:53:19 -0400
References <099b0ca2-1f5e-4eb4-a7d0-d8210bcca51a@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-98-114-97-173.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0
In-Reply-To <099b0ca2-1f5e-4eb4-a7d0-d8210bcca51a@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.115.1427226832.10327.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1427226832 news.xs4all.nl 2939 [2001:888:2000:d::a6]:36358
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:87903

Show key headers only | View raw


On 3/24/2015 2:13 PM, gdotoli@gmail.com wrote:
> I am creating a tool to search a filesystem for one simple string.
> I cannot get the syntax correct.
> Thank you in advance for your help.
>
> import sys
> import re
> import os
> path='/'
> viewfiles=os.listdir(path)

listdir is not recursive, so this code will only search files in the one 
directory, not the whole filesystem.  You need to use os.walk and modify 
the code to do the latter.

> for allfiles in viewfiles:
>      file= os.path.join(path, allfiles)
> text=open(file, "r")
> for line in text:
>      if re.match("DECRYPT_I", line):
>          print line,

You appear to have used a mixture of spaces and tabs for indents.  That 
works in 2.x, but not in 3.x.  You open but do not close files, which 
could be a problem if you open and search 100000 files in a filesystem. 
  Use a with statememt.  'allfiles' is a bad name because it get bound 
to a single file.

for file in viewfiles:
     with open(os.path.join(path, file)) as text:
         for line in text:
             if re.match("DECRYPT_I", line):
                 print(line)

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Regex Python Help gdotoli@gmail.com - 2015-03-24 11:13 -0700
  Re: Regex Python Help Skip Montanaro <skip.montanaro@gmail.com> - 2015-03-24 13:23 -0500
  Re: Regex Python Help Gary Herron <gherron@digipen.edu> - 2015-03-24 11:25 -0700
    Re: Regex Python Help Gregg Dotoli <gdotoli@gmail.com> - 2015-03-24 12:10 -0700
      Re: Regex Python Help Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-03-24 19:40 +0000
  Re: Regex Python Help Gregg Dotoli <gdotoli@gmail.com> - 2015-03-24 12:22 -0700
    Re: Regex Python Help Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> - 2015-03-24 20:40 +0100
    Re: Regex Python Help Skip Montanaro <skip.montanaro@gmail.com> - 2015-03-24 14:35 -0500
    Re: Regex Python Help Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> - 2015-03-24 20:38 +0100
    Re: Regex Python Help smap <askme.first@thankyouverymuch.invalid> - 2015-03-28 07:53 +0000
  Re: Regex Python Help Gregg Dotoli <gdotoli@gmail.com> - 2015-03-24 12:43 -0700
    Re: Regex Python Help Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-03-24 19:53 +0000
      Re: Regex Python Help Gregg Dotoli <gdotoli@gmail.com> - 2015-03-24 13:45 -0700
  Re: Regex Python Help Terry Reedy <tjreedy@udel.edu> - 2015-03-24 15:53 -0400
  Re: Regex Python Help Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-25 12:43 +1100
    Re: Regex Python Help Gregg Dotoli <gdotoli@gmail.com> - 2015-03-25 07:53 -0700
  Re: Regex Python Help Denis McMahon <denismfmcmahon@gmail.com> - 2015-03-25 20:34 +0000
    Re: Regex Python Help Gregg Dotoli <gdotoli@gmail.com> - 2015-03-25 14:19 -0700
      Re: Regex Python Help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-25 22:15 +0000
      Re: Regex Python Help Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-26 10:10 +1100
        Re: Regex Python Help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-25 23:50 +0000
        Re: Regex Python Help Terry Reedy <tjreedy@udel.edu> - 2015-03-26 01:00 -0400
      Re: Regex Python Help Denis McMahon <denismfmcmahon@gmail.com> - 2015-03-26 12:04 +0000

csiph-web