If the files are rather small, then the following simple solution would be adequate:
if os.path.isfile(file_path): # or some other condition
delete = True # Standard action: delete
try:
with open(file_path) as infile:
if "dollar" in infile.read(): # don't delete if "dollar" is found
delete = False
except IOError:
print("Could not access file {}".format(file_path))
if delete:
os.unlink(file_path)
If the files are very large and you don't want to load them entirely into memory (especially if you expect that search text to occur early in the file), replace the above with
block with the following:
with open(file_path) as infile:
for line in file:
if "dollar" in line:
delete = False
break