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

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jun 14 12:54:42 EDT 2018


On Thu, 14 Jun 2018 09:26:44 -0700, francois.rabanel wrote:

> Hi,
> 
> Here is my script :
> 
> It propose to replace some words in a file with a regular expression. It
> create a copy to write on it, and if there isn't an error, it delete the
> original by the copy with "os.rename" at the end.
> 
> 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?


> and I would to verify if the regex enter by the user, exist.

The only way to know if a regex matches the file is to try to match the 
regex and see if it matches.


[...]

> import re
> import os
> 
> try:
> 
>   path = raw_input('Please enter the path of your file that you want to
>   correct : \n')
>   print("------------------------")
>   print('Which regex ? \n')
>   regex = raw_input('----- : ')
>   print('By what ? \n')
>   new_word = raw_input('----- : ')
> 

Don't do this:

>   # Creating copy file
>   filenames_regex = re.findall(r'[a-zA-Z0-9]+\.', path)
>   filename = filenames_regex[len(filenames_regex)-1] 
>   new_filename = filename + 'copy.txt'

Do this instead:

filename, extension = os.path.splitext(path)
new_filename = filename + '.copy' + extension


>   # Replace regex by new word line by line on copy file
>   with open(path) as rf, open(new_filename, 'w') as wf:
>     for line in rf:
>       wf.write(re.sub(regex, new_word, line))
> 
> 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.

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

os.rename(new_filename, path)





-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list