osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

program that search string in text file and do something


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