I’m trying to print a line that contains specific keywords, and they both should be in the same line in order for it to print out.
This is how I approached the problem, I made 2 variables and assigned them with the variables. One of them is a filename and that changes every time the program iterated through a list.
for files in myfiles:
for lines in info:
if dir_key and files in lines:
print lines
else:
print "line not found"
This works, but the problem is, it also prints a line that only contains the filename. How do I ensure it doesn’t do that ?. I tried the
if line.beginswith(dir_key) and line.endswith(file)
but that yielded no results what so ever. so I switched to the first method.
Perhaps:
if dir_key is a string, then the test
if dir_key:will beTrueas long asdir_keyisn’t an empty string. so your total test is most likely equivalent toif True and (files in lines):