473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Replacing a txt file?

16
Hi

I have the following 2 threads in a python script
Expand|Select|Wrap|Line Numbers
  1. def recv_pkts(hdr, data):
  2.     tmp= sys.stdout
  3.     x = EthDecoder().decode(data)
  4.     sys.stdout = open('c:\\scripts\\file.txt', 'a')
  5.     print x
  6.  
  7.  
  8. while 1:
  9.  
  10.     Word = open('c:\\scripts\\file.txt').read()
  11.     v = Word.count('My Word')
  12.     if v > 2:
  13.         print "Word Here"
  14.     time.sleep(2)
  15.  
  16.  
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
Expand|Select|Wrap|Line Numbers
  1.     Word = open('c:\\scripts\\file.txt').read()
  2.     v = Word.count('My Word')
  3.     if v > 2:
  4.         print "Word Here"
  5.     time.sleep(2)
  6.         word.close()
  7.         temp = open(c:\\scripts\\file.txt','w')
  8.         temp.close()
  9.  
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
Sep 26 '07 #1
11 1843
Hi

I have the following 2 threads in a python script

Expand|Select|Wrap|Line Numbers
  1. def recv_pkts(hdr, data):
  2.     tmp= sys.stdout
  3.     x = EthDecoder().decode(data)
  4.     sys.stdout = open('c:\\scripts\\file.txt', 'a')
  5.     print x
  6.  
  7.  
  8. while 1:
  9.  
  10.     Word = open('c:\\scripts\\file.txt').read()
  11.     v = Word.count('My Word')
  12.     if v > 2:
  13.         print "Word Here"
  14.     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

Expand|Select|Wrap|Line Numbers
  1. Word = open('c:\\scripts\\file.txt').read()
  2. v = Word.count('My Word')
  3. if v > 2:
  4.     print "Word Here"
  5. time.sleep(2)
  6. word.close()
  7. temp = open(c:\\scripts\\file.txt','w')
  8. 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.
Sep 26 '07 #2
T00l
16
Thanks for the reply, I have tried it as capitals as follows but still getting the same error?

Expand|Select|Wrap|Line Numbers
  1. Word = open('c:\\scripts\\file.txt').read()
  2. v = Word.count('My Word')
  3. if v > 2:
  4.     print "Word Here"
  5. time.sleep(2)
  6. WORD.close()
  7. temp = open(c:\\scripts\\file.txt','w')
  8. temp.close()
  9.  
Any ideas?

Thanks
Sep 27 '07 #3
Thanks for the reply, I have tried it as capitals as follows but still getting the same error?

Expand|Select|Wrap|Line Numbers
  1. Word = open('c:\\scripts\\file.txt').read()
  2. v = Word.count('My Word')
  3. if v > 2:
  4.     print "Word Here"
  5. time.sleep(2)
  6. WORD.close()
  7. temp = open(c:\\scripts\\file.txt','w')
  8. temp.close()
  9.  
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.
Sep 27 '07 #4
T00l
16
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

Expand|Select|Wrap|Line Numbers
  1.     while 1:
  2.  
  3.         Word = open('c:\\scripts\\file.txt').read()
  4.         v = Word.count('Word')
  5.         if v > 2:
  6.             print "Word Here"
  7.         time.sleep(2)
  8.         Word.close()
  9.         temp = open('c:\\scripts\\file.txt','w')
  10.         temp.close()
  11.  
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?
Sep 27 '07 #5
bartonc
6,596 Expert 4TB
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

Expand|Select|Wrap|Line Numbers
  1.     while 1:
  2.  
  3.         Word = open('c:\\scripts\\file.txt').read()
  4.         v = Word.count('Word')
  5.         if v > 2:
  6.             print "Word Here"
  7.         time.sleep(2)
  8.         Word.close()
  9.         temp = open('c:\\scripts\\file.txt','w')
  10.         temp.close()
  11.  
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:
Expand|Select|Wrap|Line Numbers
  1. Word = open('c:\\scripts\\file.txt').read()
It's much better practice to use
Expand|Select|Wrap|Line Numbers
  1. fileObj = open('c:\\scripts\\file.txt')
  2. Word = fileObj.read()
  3. fileObj.close()
Sep 27 '07 #6
T00l
16
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

Expand|Select|Wrap|Line Numbers
  1. while 1:
  2.  
  3.         fileObj = open('c:\\scripts\\file.txt')
  4.         Word = fileObj.read()
  5.         v = Word.count('Word')
  6.         if v > 2:
  7.             print "Word Here"
  8.         time.sleep(2)
  9.         fileObj.close()
  10.         os.remove('c:\\scripts\\file.txt')
  11.  
But am struggling to re-create the file to before the loop starts again, I have tried the following

Expand|Select|Wrap|Line Numbers
  1.     while 1:
  2.  
  3.         fileObj = open('c:\\scripts\\file.txt')
  4.         Word = fileObj.read()
  5.         v = Word.count('Word')
  6.         if v > 2:
  7.             print "Word Here"
  8.         time.sleep(2)
  9.         fileObj.close()
  10.         os.remove('c:\\scripts\\file.txt')
  11.         temp = ('c:\\scripts\\file.txt','w')
  12.         temp.close()
  13.  
  14.  
  15.  
But am getting an error saying the file is already in use, any ideas?

Thanks
Oct 2 '07 #7
bartonc
6,596 Expert 4TB
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

Expand|Select|Wrap|Line Numbers
  1. while 1:
  2.  
  3.         fileObj = open('c:\\scripts\\file.txt')
  4.         Word = fileObj.read()
  5.         v = Word.count('Word')
  6.         if v > 2:
  7.             print "Word Here"
  8.         time.sleep(2)
  9.         fileObj.close()        os.remove('c:\\scripts\\file.txt')
  10.  
But am struggling to re-create the file to before the loop starts again, I have tried the following

Expand|Select|Wrap|Line Numbers
  1.     while 1:
  2.  
  3.         fileObj = open('c:\\scripts\\file.txt')
  4.         Word = fileObj.read()
  5.         v = Word.count('Word')
  6.         if v > 2:
  7.             print "Word Here"
  8.         time.sleep(2)
  9.         fileObj.close()
  10.         os.remove('c:\\scripts\\file.txt')
  11.         temp = ('c:\\scripts\\file.txt','w')
  12.         temp.close()
  13.  
  14.  
  15.  
But am getting an error saying the file is already in use, any ideas?

Thanks
Just a couple of points:
Expand|Select|Wrap|Line Numbers
  1. # code tags need something in col 0 to work right
  2.     while 1:
  3.         fileObj = open('c:\\scripts\\file.txt')
  4.         Word = fileObj.read()
  5.         # close as soon as possible #
  6.         fileObj.close()
  7.  
  8.         v = Word.count('Word')
  9.         if v > 2:
  10.             print "Word Here"
  11.         time.sleep(2)
  12.          # Don't need this. open() in write mode will overwrite (I think)
  13.         os.remove('c:\\scripts\\file.txt')
  14.  
  15.         # Correction: open() #
  16.         temp = open('c:\\scripts\\file.txt','w')
  17.         temp.close() # less whitespace, please
So when do you break out of the loop, anyway??????
Oct 2 '07 #8
T00l
16
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

Expand|Select|Wrap|Line Numbers
  1. #
  2.     while 1:
  3.  
  4.         fileObj = open('c:\\scripts\\file.txt')
  5.         Word = fileObj.read()
  6.         v = Word.count('Word')
  7.         fileObj.close()
  8.         if v > 2:
  9.             print "Word Here"
  10.         time.sleep(2)
  11.         temp = open ('c:\\scripts\\file.txt','w')
  12.         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?
Oct 3 '07 #9
bartonc
6,596 Expert 4TB
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

Expand|Select|Wrap|Line Numbers
  1. #
  2.     while 1:
  3.  
  4.         fileObj = open('c:\\scripts\\file.txt')
  5.         Word = fileObj.read()
  6.         v = Word.count('Word')
  7.         fileObj.close()
  8.         if v > 2:
  9.             print "Word Here"
  10.         time.sleep(2)
  11.         temp = open ('c:\\scripts\\file.txt','w')
  12.         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:
Expand|Select|Wrap|Line Numbers
  1. >>> temp = open ('file.txt','w')
  2. >>> temp.close()
  3. >>> 
Try:
Expand|Select|Wrap|Line Numbers
  1. # ...
  2.         time.sleep(2)
  3.         temp = open ('c:\\scripts\\file.txt','w')
  4.         print temp
It must look like
('c:\\scripts\\file.txt', 'w')
to make any sense at all.
Oct 3 '07 #10
T00l
16
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
Oct 3 '07 #11
bartonc
6,596 Expert 4TB
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.
Oct 4 '07 #12

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

Similar topics

13
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...
3
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...
12
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 ?
7
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...
12
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: ...
32
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...
2
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...
9
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...
6
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>...
7
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>...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
BarryA
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...
1
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...
0
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...
0
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,...
0
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,...
0
jinu1996
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...

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.