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

Cameron Simpson cs at cskk.id.au
Thu Jun 14 20:00:59 EDT 2018


On 14Jun2018 16:54, Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
>On Thu, 14 Jun 2018 09:26:44 -0700, francois.rabanel wrote:
>> My problem is, if I work on a huge file, I'll try to avoid to read the
>> file because it will be crash my computer :)
>
>How does reading a file crash your computer?

Likely because he tried to read the whole file into memory and match against 
it. Guessing:

  text = open(the_filename).read()
  ... search the text for the regexp ...

Francois, unless your regex can cross multiple lines it is better to search 
files like this:

  with open(the_filename) as f:
    for line in f:
      ... search the line for the regexp ...

That way you only need to keep one line at a time in memory.

>> except OSError:
>>   print("Permission denied")
>
>That's not what OSError means. OSError can mean many different things.
>That's why it isn't called "PermissionDeniedError".
>
>You need to look at the exception to see what caused it, not just assume
>it was a permissions error.
>
>> except IOError:
>>   print("This file doesn't exist")
>
>That's not what IOError means either. That is why it isn't called
>FileDoesntExistError. Again, you need to look at the exception to see
>what the error actually is.

In particular, you should always _either_ inspect the exception to see what 
went wrong and handle it, _or_ include the exception text in your error 
message, for example:

  except IOError as e:
    print("IO Error on file:", e)

That way an unhandled exception gets reported.

>> else:
>>   os.rename(new_filename, filename + 'txt')
>
>os.rename(new_filename, path)

Importantly:

  os.rename(path, new_filename)

The old name comes first, then the new name.

Also, you might want to ensure that new_filename doesn't already exist...

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Python-list mailing list