Hi
I have the following 2 threads in a python script -
def recv_pkts(hdr, data):
-
tmp= sys.stdout
-
x = EthDecoder().decode(data)
-
sys.stdout = open('c:\\scripts\\file.txt', 'a')
-
print x
-
-
-
while 1:
-
-
Word = open('c:\\scripts\\file.txt').read()
-
v = Word.count('My Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
-
The first thread writes to a file and the 2nd reads the same file and checks for 'My Word' I am trying to replace the file.txt with a new file.txt after the time.sleep(2) by the doing the following -
Word = open('c:\\scripts\\file.txt').read()
-
v = Word.count('My Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
word.close()
-
temp = open(c:\\scripts\\file.txt','w')
-
temp.close()
-
But i'm getting the following error
Unhandled exception in thread started by <function wordcount at 0x00BA0030>
Traceback (most recent call last):
File "keep.py", line 18, in wordcount
word.close()
NameError: global name 'word' is not defined
Has anyone got any ideas why this is happening?
Thanks
11 1843
Hi
I have the following 2 threads in a python script - def recv_pkts(hdr, data):
-
tmp= sys.stdout
-
x = EthDecoder().decode(data)
-
sys.stdout = open('c:\\scripts\\file.txt', 'a')
-
print x
-
-
-
while 1:
-
-
Word = open('c:\\scripts\\file.txt').read()
-
v = Word.count('My Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
The first thread writes to a file and the 2nd reads the same file and checks for 'My Word' I am trying to replace the file.txt with a new file.txt after the time.sleep(2) by the doing the following - Word = open('c:\\scripts\\file.txt').read()
-
v = Word.count('My Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
word.close()
-
temp = open(c:\\scripts\\file.txt','w')
-
temp.close()
But i'm getting the following error
Unhandled exception in thread started by <function wordcount at 0x00BA0030>
Traceback (most recent call last):
File "keep.py", line 18, in wordcount
word.close()
NameError: global name 'word' is not defined
Has anyone got any ideas why this is happening?
Thanks
You need to capitalize "word" when you close the file. Watch out, it also looks like your missing a quote in your second to last line of code. Make sure to use CODE tags when you post so your code is easier to read.
Thanks for the reply, I have tried it as capitals as follows but still getting the same error? -
Word = open('c:\\scripts\\file.txt').read()
-
v = Word.count('My Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
WORD.close()
-
temp = open(c:\\scripts\\file.txt','w')
-
temp.close()
-
Any ideas?
Thanks
Thanks for the reply, I have tried it as capitals as follows but still getting the same error? -
Word = open('c:\\scripts\\file.txt').read()
-
v = Word.count('My Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
WORD.close()
-
temp = open(c:\\scripts\\file.txt','w')
-
temp.close()
-
Any ideas?
Thanks
Sorry, I meant only the first letter of "word", as in "Word". The point is to be consistent with the capitalization in variable names since they are case-sensitive.
Thanks, I've tried that but getting a different error now
Unhandled exception in thread started by <function wordcount at 0x00BA0030>
Traceback (most recent call last):
File "keep.py", line 18, in wordcount
Word.close()
AttributeError: 'str' object has no attribute 'close'
I was using the following code -
while 1:
-
-
Word = open('c:\\scripts\\file.txt').read()
-
v = Word.count('Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
Word.close()
-
temp = open('c:\\scripts\\file.txt','w')
-
temp.close()
-
I think it may to do with the way the file is being opened in
Word = open('c:\\scripts\\file.txt').read()
instead of
Word = open('c:\\scripts\\file.txt','r')
However I can't get the latter to work as a Thread?
Thanks, I've tried that but getting a different error now
Unhandled exception in thread started by <function wordcount at 0x00BA0030>
Traceback (most recent call last):
File "keep.py", line 18, in wordcount
Word.close()
AttributeError: 'str' object has no attribute 'close'
I was using the following code -
while 1:
-
-
Word = open('c:\\scripts\\file.txt').read()
-
v = Word.count('Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
Word.close()
-
temp = open('c:\\scripts\\file.txt','w')
-
temp.close()
-
I think it may to do with the way the file is being opened in
Word = open('c:\\scripts\\file.txt').read()
instead of
Word = open('c:\\scripts\\file.txt','r')
However I can't get the latter to work as a Thread?
Too many operations on this line: - Word = open('c:\\scripts\\file.txt').read()
It's much better practice to use - fileObj = open('c:\\scripts\\file.txt')
-
Word = fileObj.read()
-
fileObj.close()
Thanks for the reply
I have tried as you suggested and it works great, I have added another line of code to remove the txt file as shown below -
while 1:
-
-
fileObj = open('c:\\scripts\\file.txt')
-
Word = fileObj.read()
-
v = Word.count('Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
fileObj.close()
-
os.remove('c:\\scripts\\file.txt')
-
But am struggling to re-create the file to before the loop starts again, I have tried the following -
while 1:
-
-
fileObj = open('c:\\scripts\\file.txt')
-
Word = fileObj.read()
-
v = Word.count('Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
fileObj.close()
-
os.remove('c:\\scripts\\file.txt')
-
temp = ('c:\\scripts\\file.txt','w')
-
temp.close()
-
-
-
But am getting an error saying the file is already in use, any ideas?
Thanks
Thanks for the reply
I have tried as you suggested and it works great, I have added another line of code to remove the txt file as shown below -
while 1:
-
-
fileObj = open('c:\\scripts\\file.txt')
-
Word = fileObj.read()
-
v = Word.count('Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
fileObj.close() os.remove('c:\\scripts\\file.txt')
-
But am struggling to re-create the file to before the loop starts again, I have tried the following -
while 1:
-
-
fileObj = open('c:\\scripts\\file.txt')
-
Word = fileObj.read()
-
v = Word.count('Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
fileObj.close()
-
os.remove('c:\\scripts\\file.txt')
-
temp = ('c:\\scripts\\file.txt','w')
-
temp.close()
-
-
-
But am getting an error saying the file is already in use, any ideas?
Thanks
Just a couple of points: - # code tags need something in col 0 to work right
-
while 1:
-
fileObj = open('c:\\scripts\\file.txt')
-
Word = fileObj.read()
-
# close as soon as possible #
-
fileObj.close()
-
-
v = Word.count('Word')
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
# Don't need this. open() in write mode will overwrite (I think)
-
os.remove('c:\\scripts\\file.txt')
-
-
# Correction: open() #
-
temp = open('c:\\scripts\\file.txt','w')
-
temp.close() # less whitespace, please
So when do you break out of the loop, anyway??????
Thanks, the loop doesn't break out, it constanty loops until you exit the script, the wait time will increase to 30secs though so it won't be replacing the file every secs.
I have now got the following - #
-
while 1:
-
-
fileObj = open('c:\\scripts\\file.txt')
-
Word = fileObj.read()
-
v = Word.count('Word')
-
fileObj.close()
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
temp = open ('c:\\scripts\\file.txt','w')
-
temp.close()
But am getting this error...
Unhandled exception in thread started by <function wordcount at 0x00B9E030>
Traceback (most recent call last):
File "keep.py", line 21, in wordcount
temp.close()
AttributeError: 'tuple' object has no attribute 'close'
Which i can't understand why its not reconising temp.close?
Thanks, the loop doesn't break out, it constanty loops until you exit the script, the wait time will increase to 30secs though so it won't be replacing the file every secs.
I have now got the following - #
-
while 1:
-
-
fileObj = open('c:\\scripts\\file.txt')
-
Word = fileObj.read()
-
v = Word.count('Word')
-
fileObj.close()
-
if v > 2:
-
print "Word Here"
-
time.sleep(2)
-
temp = open ('c:\\scripts\\file.txt','w')
-
temp.close()
But am getting this error...
Unhandled exception in thread started by <function wordcount at 0x00B9E030>
Traceback (most recent call last):
File "keep.py", line 21, in wordcount
temp.close()
AttributeError: 'tuple' object has no attribute 'close'
Which i can't understand why its not reconising temp.close?
Me neither. I just tried it in the current working directory, and it works: -
>>> temp = open ('file.txt','w')
-
>>> temp.close()
-
>>>
Try: - # ...
-
time.sleep(2)
-
temp = open ('c:\\scripts\\file.txt','w')
-
print temp
It must look like
('c:\\scripts\\file.txt', 'w')
to make any sense at all.
Not sure what happened but I re-typed it and its working fine now, must of had a space in there somewhere!
Thanks for all your help
Not sure what happened but I re-typed it and its working fine now, must of had a space in there somewhere!
Thanks for all your help
"Re=typed", huh? Normally we get copy & paste anomalies.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: yaipa |
last post by:
What would be the common sense way of finding a binary pattern in a
..bin file, say some 200 bytes, and replacing it with an updated pattern
of the same length at the same offset?
Also, the...
|
by: dornick |
last post by:
So I want to do the above, and I really, REALLY don't want to rewrite
the entire file. I've been working on it for a while now, and can't
for the life of me get it functioning. Basically, I want...
|
by: anonymous |
last post by:
Hello,
I need to replace this char  with another char.
However I am not able to acieve this. I tried this but it
doesnt work:
str = str.Replace(chr(asc(194)), "")
Can somebody help ?
|
by: Ryan Taylor |
last post by:
Hi.
I have some code that dynamically generates a PDF and spits this content
directly to the web browser. I use HTMLDoc to create the Pdf's from html. So
the user can click on a button "Print...
|
by: Adam J. Schaff |
last post by:
I am writing a quick program to edit a binary file that contains file paths
(amongst other things). If I look at the files in notepad, they look like:
...
|
by: FireHead |
last post by:
Hello C World & Fanatics
I am trying replace fgets and provide a equavivalant function of
BufferedInputReader::readLine.
I am calling this readLine function as get_Stream.
In the line 4 where...
|
by: David |
last post by:
Sent this to alt.php a couple of days back, but doesn't look like I'll
get an answer, so trying here.
I'm trying to convert a script to use friendly URLs, I've done this
before, but my PHP...
|
by: Prakash Singh Bhakuni |
last post by:
am replacing the default "Browse..." button for input type=file. This works fine except that the form will only submit after the SUBMIT button is clicked twice.
Any ideas on why this is happening...
|
by: saif.shakeel |
last post by:
Hi,
I need to replace a string in xml file with something else.Ex
- <SERVICEPARAMETER id="_775" Semantics="subfunction" DDORef="_54">
<SHORTNAME>rate</SHORTNAME>
<LONGNAME>rate</LONGNAME>...
|
by: saif.shakeel |
last post by:
Hi,
I need to replace a string in xml file with something else.Ex
- <SERVICEPARAMETER id="_775" Semantics="subfunction" DDORef="_54">
<SHORTNAME>rate</SHORTNAME>
<LONGNAME>rate</LONGNAME>...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |