program that search string in text file and do something

ast nomail at com.invalid
Fri Aug 4 05:26:27 EDT 2017


<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")







More information about the Python-list mailing list