Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87903
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Regex Python Help |
| Date | 2015-03-24 15:53 -0400 |
| References | <099b0ca2-1f5e-4eb4-a7d0-d8210bcca51a@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.115.1427226832.10327.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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