program that search string in text file and do something

alon.najman at gmail.com alon.najman at gmail.com
Fri Aug 4 06:08:52 EDT 2017


On Friday, August 4, 2017 at 12:27:02 PM UTC+3, ast wrote:
> <alon.najman at gmail.com> a écrit dans le message de 
> news:b6cc4ee5-71be-4550-be3e-59ebeee7a01b at googlegroups.com...
> > Hi, I'm new to thing forum and to this programming in python!
> >
> > can someone help me and write me how to write a program that do:
> > - search for a string in certain text file and if it founds the string it delete the file? and 
> > print something?
> >
> > thanks.
> 
> import os
> pattern = "azerty"
> found = False
> 
> with open("foo.txt", "r") as f:
>     for line in f:
>         if pattern in line:
>             found = True
>             break
> 
> if found:
>     os.remove("foo.txt")

thanks man! that works



More information about the Python-list mailing list