LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-22-2007, 03:00 PM   #1
chess
Member
 
Registered: Mar 2002
Location: 127.0.0.1
Distribution: Slackware and OpenBSD
Posts: 740

Rep: Reputation: 190Reputation: 190
Python: search for string in a list or file


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!
 
Old 08-22-2007, 03:52 PM   #2
gizmo_thunder
Member
 
Registered: Apr 2004
Posts: 101

Rep: Reputation: 15
the code seems correct .. the only problem i see is that
the strings in the files have a newline character at the end
of them (you cannot see it but it will be there) so try appending
a "\n" to your newfile string.. that should do the trick.
 
Old 08-22-2007, 04:05 PM   #3
paddy3118
LQ Newbie
 
Registered: Jul 2007
Location: Bristol UK
Distribution: RedHat, Cygwin
Posts: 14

Rep: Reputation: 0
Quote:
Originally Posted by chess View Post
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()
<<SNIP>>
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!
Code:
allfiles = set(f.strip() for f in filelist)
if filename not in allfiles:
  do_something()
Should do it

- Paddy.
 
Old 08-22-2007, 04:22 PM   #4
chess
Member
 
Registered: Mar 2002
Location: 127.0.0.1
Distribution: Slackware and OpenBSD
Posts: 740

Original Poster
Rep: Reputation: 190Reputation: 190
Excellent, thanks to both of you. I had not even thought about the fact the newline was at the end of each string in the file. I tried both solutions and they worked great. I appreciate the assistance.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
giving search-string(s) from a file to linux "find" command Fond_of_Opensource Linux - Newbie 3 02-02-2009 06:14 PM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
Grep String Search, and identify source file. carl0ski Linux - General 4 01-21-2006 08:15 AM
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
[c++] Search in a string hylke Programming 5 06-14-2004 11:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration