Hello --
I'm working on a Python script and I have run into a roadblock that I cannot seem to overcome despite hours of google searches and trying different things. I am sure this is a simple matter, so I apologize in advance. Here is what I am trying to do.
I have a text file that has a list of filenames, one per line:
file1.txt
file2.txt
file3.txt
etc.
I have read this file into a list like so:
Code:
files = open('filenames.txt', 'r')
filelist = files.readlines()
files.close()
Now, I am trying to create a condition where something is done only if a certain string containing a new filename, say file4.txt, is _not_ in the list. For purposes of this example, the string is called "newfile". Anyway, here are some of the things I have tried:
Code:
if newfile not in filelist:
(do stuff)
for found in filelist:
if found.find(newfile) != 1:
(do stuff)
for line in filelist:
if line.find(newfile) :
pass
else:
(do stuff)
for line in filelist:
if line != newfile:
(do stuff)
What is the right form of conditional for doing something if a string cannot be found in a list? None of these seem to work. Any assistance would be greatly appreciated. Thanks!