Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify python string with contain method [duplicate]

Tags:

python-2.7

I want it to find the following file in mylist: "Microsoft Word 105Prt" (this file name could vary but will always have "Word" in it.

for myfile in filelist:
    if myfile.contains("Word"): 
        print myfile

How can I modify this to work in python 2.7.5 since contains doesn't work.

like image 244
user12059 Avatar asked Apr 24 '14 00:04

user12059


2 Answers

You can substitute find for contains and just check for a return code of something other than -1.

for myfile in filelist:
    if myfile.find("Word")!=-1: 
        print myfile
like image 68
Sam A Avatar answered Oct 14 '22 18:10

Sam A


You can simply use the in keyword, like so:

if 'Word' in myfile:
    print myfile
like image 40
aruisdante Avatar answered Oct 14 '22 19:10

aruisdante