473,553 Members | 2,957 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looping a script

16 New Member
Hi

I have the following script that counts the amount of words in a file, prints an output, closes the file and then waits for 10secs. I need the script to repeat itself after it was waited the 10secs, anyone know how I can do this?

import time
text = open("c:\\file. txt").read()
v = text.count('wor d')
if v > 2:
print "has words"
text.close()
time.sleep(10)

Thanks
Aug 5 '07 #1
6 4659
ilikepython
844 Recognized Expert Contributor
Hi

I have the following script that counts the amount of words in a file, prints an output, closes the file and then waits for 10secs. I need the script to repeat itself after it was waited the 10secs, anyone know how I can do this?

import time
text = open("c:\\file. txt").read()
v = text.count('wor d')
if v > 2:
print "has words"
text.close()
time.sleep(10)

Thanks
Are you familiar with loops? Check some tutorials online if not.:
Expand|Select|Wrap|Line Numbers
  1. while 1:
  2.     text = open("c:\\file.txt").read()
  3.     v = text.count('word')
  4.     if v > 2:
  5.         print "has words"
  6.     text.close()
  7.     time.sleep(10)
  8.  
Aug 5 '07 #2
T00l
16 New Member
Hi

Thanks for the reply, i'm learning python/programming from a couple of books but couldn't see that anywhere, however after your post and looking in the books again, its staring me in the face!

Now that I have that loop going, I want to add it to an existing looping script. However the first loop is continuous so the program does not seem to be executing my second loop code, can you tell me if there is a better loop than the p.loop for the first section of code to allow it (While still looping) to continue and execute the second loop part of code?

import sys
import pcapy
import time
import time
from impacket.Impact Decoder import *

def recv_pkts(hdr, data):
While 1:
x = EthDecoder().de code(data)
sys.stdout = open('c:\\dir\\ file.txt', 'a')
print x


def get_int():
devs = pcapy.findallde vs()
i=0
for eth in devs:
print " %d - %s" %(i,devs[i])
i+=1
sel=input(" Select interface: ")
dev=devs[sel]
return dev

dev = get_int()
p = pcapy.open_live (dev, 1500, 0, 100)
p.setfilter('tc p')
print "Listening on eth: net=%s, mask=%s\n" % (p.getnet(), p.getmask())
p.loop(-1, recv_pkts)

while 1:
text = open("c:\\dir\\ file.txt").read ()
v = text.count('wor d')
if v > 2:
print "has words"
text.close()
time.sleep(10)
Aug 7 '07 #3
T00l
16 New Member
Sorry, the while: 1 on line 8 should not be there, the function is

def recv_pkts(hdr, data):
x = EthDecoder().de code(data)
sys.stdout = open('c:\\dir\\ file.txt', 'a')
print x
Aug 7 '07 #4
ilikepython
844 Recognized Expert Contributor
Hi

Thanks for the reply, i'm learning python/programming from a couple of books but couldn't see that anywhere, however after your post and looking in the books again, its staring me in the face!

Now that I have that loop going, I want to add it to an existing looping script. However the first loop is continuous so the program does not seem to be executing my second loop code, can you tell me if there is a better loop than the p.loop for the first section of code to allow it (While still looping) to continue and execute the second loop part of code?

import sys
import pcapy
import time
import time
from impacket.Impact Decoder import *

def recv_pkts(hdr, data):
x = EthDecoder().de code(data)
sys.stdout = open('c:\\dir\\ file.txt', 'a')
print x


def get_int():
devs = pcapy.findallde vs()
i=0
for eth in devs:
print " %d - %s" %(i,devs[i])
i+=1
sel=input(" Select interface: ")
dev=devs[sel]
return dev

dev = get_int()
p = pcapy.open_live (dev, 1500, 0, 100)
p.setfilter('tc p')
print "Listening on eth: net=%s, mask=%s\n" % (p.getnet(), p.getmask())
p.loop(-1, recv_pkts)

while 1:
text = open("c:\\dir\\ file.txt").read ()
v = text.count('wor d')
if v > 2:
print "has words"
text.close()
time.sleep(10)
I am not familiar with the pcapy module but you could try using threads:
Expand|Select|Wrap|Line Numbers
  1. import thread
  2. def wordcount():
  3.     while 1:
  4.         text = open("c:\\dir\\file.txt").read()
  5.         v = text.count('word')
  6.         if v > 2:
  7.             print "has words"
  8.         text.close()
  9.         time.sleep(10)
  10.  
  11. def recv_pkts(hdr, data):
  12.     x = EthDecoder().decode(data)
  13.     sys.stdout = open('c:\\dir\\file.txt', 'a')
  14.     print x
  15.  
  16.  
  17. def get_int():
  18.     devs = pcapy.findalldevs()
  19.     for (i, eth) in enumerate(devs):  # no need for extra variable
  20.         print " %d - %s" %(i,devs[i])
  21.  
  22.     sel=input(" Select interface: ")
  23.     dev=devs[sel]
  24.     return dev
  25.  
  26. dev = get_int()
  27. p = pcapy.open_live(dev, 1500, 0, 100)
  28. p.setfilter('tcp')
  29. print "Listening on eth: net=%s, mask=%s\n" % (p.getnet(), p.getmask())
  30. thread.start_new_thread(wordcount, ())
  31. p.loop(-1, recv_pkts)
  32.  
Aug 7 '07 #5
T00l
16 New Member
Hi

Thanks for that, I haven't looked at threading before but will look into it further now.
I think the problem i'll have is that the def recv_pkts needs to run continually where as the def wordcount() needs to only run every 10 secs, it looks from the modiftication you have done the whole script will wait 10 secs?
Is this correct or have I got that wrong?
Aug 7 '07 #6
ilikepython
844 Recognized Expert Contributor
Hi

Thanks for that, I haven't looked at threading before but will look into it further now.
I think the problem i'll have is that the def recv_pkts needs to run continually where as the def wordcount() needs to only run every 10 secs, it looks from the modiftication you have done the whole script will wait 10 secs?
Is this correct or have I got that wrong?
Nope, that's the thing about threads, they run in parallel. The time.sleep call only works for its thread so the main script will keep on running and only the thread will sleep.

There is also a threading module that you can also use. Make sure to check the documentation for both modules to get a better understanding.
Aug 7 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
4045
by: kaptain kernel | last post by:
i've got a while loop thats iterating through a text file and pumping the contents into a database. the file is quite large (over 150mb). the looping causes my CPU load to race up to 100 per cent. Even if i remove the mysql insert query and just loop through the file , it still hits 100 per cent CPU. This has the knock on effect of slowing...
2
2564
by: Ivo | last post by:
Hi, I have an audio file (.mid or .wav or .mp3) in an object element: <object id="snd" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf. cab#Version=5,1,52,701" type="application/x-oleobject" width="282" height="53"> <param name="FileName"...
11
2590
by: Lyle Fairfield | last post by:
The stored procedure script below is an example of how looping, case statements and output parameters can be used in MS-SQL stored procedures to accomplish things for which we may have had to use VBA code, or automation, in the JET world. Some who have not yet worked with MS-SQL may be interested. The script is indented in reality but I have...
5
1477
by: Jeff | last post by:
Hey gang. I have a script that gets stats from a mssql db, and then inserts those stats into a temp table. where i can work with them as i wish. the problem is it isn't looping through all the records, and i am not sure why. i know the script isn't perfect, as it is open to sql injection and stuff like that, but once the site is...
2
6242
by: Bart Van Hemelen | last post by:
The situation: I have a CheckBoxList cblTest, the items are disabled in cblTest_DataBound in a foreach (ListItem oItem in cblTest.Items) loop. I provide a link that calls a client-side JavaScript that enables the items -- this works perfectly. However, when I then click a LinkButton that does a postback and I loop through the items on...
1
2946
by: Alex | last post by:
Hello, I have a stored procedure that processes an individual file from a directory and archives it in a subdirectory.Now, the problem is, when i execute it , it will only process one file. What i want to do is to check to see if there are any files in the folder, and if there are , process them all, and once done, go to the next part in a...
6
1807
by: Luke - eat.lemons | last post by:
Hi, Im pretty new to asp so all light on this question would be great. Basically i need to test to see what value is set (where to retrieve the data from) so ive done it like this: If Request.Querystring("id") = "" then TidF=Request.Form("TidF") Else
0
1953
by: anthon | last post by:
Hi all - first post! anywho; I need to create a function for speeding up and down a looping clip. imagine a rotating object, triggered by an action, and slowly decreasing in speed, till it reaches a point 0 (compare a hand spinning a fortune wheel). now, this is quite an easy this to achieve, since you just have to set an interval to...
20
2802
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1 then
3
1943
by: chiku1523 | last post by:
Hi, Please find the following code. In function setAnswers, I am looping with each question. I have inner loop, which is looping for each answers of the questions. If any of the answer for question 1 and question2 is selected this code is working fine. But if None of answer of either question selected, it is not iterating through outer loop....
0
7568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7772
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6109
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5133
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1990
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1106
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.