How can I verify if the regex exist in a file without reading ?

francois.rabanel at outscale.com francois.rabanel at outscale.com
Fri Jun 15 08:33:31 EDT 2018


Le vendredi 15 juin 2018 12:36:40 UTC+2, Steven D'Aprano a écrit :
> On Fri, 15 Jun 2018 01:01:03 -0700, francois.rabanel wrote:
> 
> > I work with a file which contains millions lines, a simply file.read()
> > and I'm running out of memory
> 
> Assuming each line is on average a hundred characters long, a million 
> lines is (approximately) 100 MB. Even on a computer with only 2GB of 
> memory, you should be able to read 100 MB.
> 
> But you shouldn't: it is much better to process the file line by line.
> 
> 
> # Don't do this:
> with open(pathname) as f:
>     text = f.read()  # Slurp the entire file into memory at once.
>     ...
> 
> # Do this instead
> with open(pathname) as f:
>     for line in f:
>         # process one line at a time
> 
> 
> You said you are running out of memory, earlier you said the computer was 
> crashing... please describe exactly what happens. If you get a Traceback, 
> copy and paste the entire message.
> 
> (Not just the last line.)
> 
> 
> 
> 
> -- 
> Steven D'Aprano
> "Ever since I learned about confirmation bias, I've been seeing
> it everywhere." -- Jon Ronson

I resolve my problem and when I look to my solution I don't understand why I didn't do it earlier :)


with open(path) as file:
  result = []
  for line in file:
    find_regex  = re.search(regex,line)
    if find_regex:
      result.append(find_regex.group())
  if len(result) == 0:
    sys.exit('Regex not found')
  elif result[0] == '':
    sys.exit('Whitespace as regex don\'t work')


I was looking for a way to check if the regex's user was correct or not



More information about the Python-list mailing list