Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 14th, 2003, 02:13 PM
Random Random is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 72 Random User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 23 sec
Reputation Power: 10
Search a file for text

Hello!

I have just started to learn python and wanted to write my first useful script. At work I do a search thru very large web log files.
So I wanted to create a script that would work like M$ search
text in files, where I would list a path and have all files where
the text occurs be written in separate log file. So far my search
in Google has not produced any sample code that would show
how to acomplish this.
Still learning and trying
tia

Reply With Quote
  #2  
Old August 14th, 2003, 03:17 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Hi random,

This shouldn't be very hard. I'm assuming that the log's are all in the same dir? if so then it's even easier!

Code:
#!/usr/bin/env python

path = 'directory to search'
string = 'search string'

found = []

#open path and read a list of files
for file in os.listdir(path):
    #if 'file' is a file
    if os.path.isfile(path + file):
        #see if string is in file
        if string in open(path + file, 'r').read():
          #add file to found list.
            found.append(file)

#print files that contain the search text.            
for file in found:
    print file

This is just the basic idea, I havn't tested this but it should work. let me knwo if it doesn't

Hope this some help,
Mark.

Reply With Quote
  #3  
Old August 15th, 2003, 11:30 PM
Random Random is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 72 Random User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 23 sec
Reputation Power: 10
Hi Mark,

Thanks for the speedy reply. As I am just starting to learn python
the comments that you have provided are going to be extremly
helpful for me to understand and learn. I have a couple of web logs at home which I would try this on. Will let you know?

Again thanks alot.
Random

Reply With Quote
  #4  
Old August 18th, 2003, 10:04 AM
Random Random is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 72 Random User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 23 sec
Reputation Power: 10
Hi Mark!

I am getting an error by running this script, and not quite sure how to fix it?

tia
random

File "D:\Python\search.py", line 13, in ?
if string in open(path + file, 'r').read():
TypeError: 'in <string>' requires character as left operand

Reply With Quote
  #5  
Old August 18th, 2003, 10:48 AM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
Send a message via MSN to SolarBear
Just in case : have you changed the values of 'directory to search' and 'search string' ? I know it happens to me sometimes.

And if you did, what were those directory and search string ? Maybe you put some illegal character or whatnot. Just put 'em here.

Reply With Quote
  #6  
Old August 18th, 2003, 11:07 AM
Random Random is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 72 Random User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 23 sec
Reputation Power: 10
All I did was to add

import os
path = 'C:/'
string = 'foo'

Was there anything that I forgot to change or supply?

Reply With Quote
  #7  
Old August 18th, 2003, 01:44 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I recreated the program above and it works fine for me (I've attached the source code). I've never seen that error before but if you post your code i'll take a look.. What version of Python are you using anyway?

It may be worth saying that path's in windows use '\' instead of '/' (you'll have to escape backslashes ('\\') when you use them in strings)

Have fun,
Mark.
Attached Files
File Type: py search.py (255 Bytes, 602 views)

Reply With Quote
  #8  
Old August 18th, 2003, 01:58 PM
Random Random is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 72 Random User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 23 sec
Reputation Power: 10
Thanks for trying to help me get this code running, but for me it does not work. I took your code and copied it exactly like you did
and ran it. My version of python is 2.2.

Error message

C:\>search.py
Traceback (most recent call last):
File "C:\search.py", line 12, in ?
if string in open(path + file, 'rb').read():
TypeError: 'in <string>' requires character as left operand

Reply With Quote
  #9  
Old August 18th, 2003, 02:23 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I'm running Python 2.3, maybe this is something to do with that.. you could try renaming the string variable (although string isn't a reserved word).

Alternativle you could try using count instead of..

if open(path + file, 'rb').read().count(string) != 0: found.append(file)

Mark.

Reply With Quote
  #10  
Old August 18th, 2003, 02:44 PM
Random Random is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 72 Random User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 47 m 23 sec
Reputation Power: 10
Upgrading to 2.3 made the error go away.
Thanks

Reply With Quote
  #11  
Old August 18th, 2003, 02:57 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Very welcome, glad I could help. 2.3 is very nice hope you like it, it's faster than 2.2, which seems to be the main improvment. That and a few nice module additions and improvments.

http://forums.devshed.com/t73300/s.html

Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Search a file for text

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap