Fabbri Systems Forum Index Fabbri Systems
Fabbri Systems Technical Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Search a file in python

 
Post new topic   Reply to topic    Fabbri Systems Forum Index -> Python Programming

 

 

 

 

View previous topic :: View next topic  
Author Message
admin
Site Admin


Joined: 28 Feb 2006
Posts: 24

PostPosted: Sat Oct 04, 2008 3:39 am    Post subject: Search a file in python Reply with quote

Code:



filename = "/tmp/test"
search_word = "blah"

file = open(filename)
line_num = 0
found = 0
for l in file.readlines() :
        line_num += 1
        index = l.find(search_word)
        if index >= 0 :
                print ("Found '%s' at line %d, character %d" %
                        (search_word, line_num, index))
                found +=1

print "Found %d instances of '%s' in %s." % (found, search_word, filename)

Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Fabbri Systems Forum Index -> Python Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group