Replacing a txt file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • T00l
    New Member
    • Jul 2007
    • 16

    Replacing a txt file?

    Hi

    I have the following 2 threads in a python script
    Code:
    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
    Code:
    	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
    Last edited by bartonc; Sep 27 '07, 07:12 AM. Reason: Added [CODE][/CODE] tags.
  • KaezarRex
    New Member
    • Sep 2007
    • 52

    #2
    Originally posted by T00l
    Hi

    I have the following 2 threads in a python script

    [CODE=python]def recv_pkts(hdr, data):
    tmp= sys.stdout
    x = EthDecoder().de code(data)
    sys.stdout = open('c:\\scrip ts\\file.txt', 'a')
    print x


    while 1:

    Word = open('c:\\scrip ts\\file.txt'). read()
    v = Word.count('My Word')
    if v > 2:
    print "Word Here"
    time.sleep(2)[/CODE]


    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

    [CODE=python]Word = open('c:\\scrip ts\\file.txt'). read()
    v = Word.count('My Word')
    if v > 2:
    print "Word Here"
    time.sleep(2)
    word.close()
    temp = open(c:\\script s\\file.txt','w ')
    temp.close()[/CODE]


    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.

    Comment

    • T00l
      New Member
      • Jul 2007
      • 16

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

      Code:
      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

      Comment

      • KaezarRex
        New Member
        • Sep 2007
        • 52

        #4
        Originally posted by T00l
        Thanks for the reply, I have tried it as capitals as follows but still getting the same error?

        Code:
        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.

        Comment

        • T00l
          New Member
          • Jul 2007
          • 16

          #5
          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

          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:\\scrip ts\\file.txt'). read()

          instead of

          Word = open('c:\\scrip ts\\file.txt',' r')

          However I can't get the latter to work as a Thread?

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by T00l
            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

            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:\\scrip ts\\file.txt'). read()

            instead of

            Word = open('c:\\scrip ts\\file.txt',' r')

            However I can't get the latter to work as a Thread?
            Too many operations on this line:[CODE=python]Word = open('c:\\scrip ts\\file.txt'). read()[/CODE]It's much better practice to use[CODE=python]fileObj = open('c:\\scrip ts\\file.txt')
            Word = fileObj.read()
            fileObj.close()[/CODE]

            Comment

            • T00l
              New Member
              • Jul 2007
              • 16

              #7
              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

              Code:
              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

              Code:
              	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

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by T00l
                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

                Code:
                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

                Code:
                	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:
                # 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??????

                Comment

                • T00l
                  New Member
                  • Jul 2007
                  • 16

                  #9
                  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

                  [CODE=python]#
                  while 1:

                  fileObj = open('c:\\scrip ts\\file.txt')
                  Word = fileObj.read()
                  v = Word.count('Wor d')
                  fileObj.close()
                  if v > 2:
                  print "Word Here"
                  time.sleep(2)
                  temp = open ('c:\\scripts\\ file.txt','w')
                  temp.close()[/CODE]

                  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?

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #10
                    Originally posted by T00l
                    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

                    [CODE=python]#
                    while 1:

                    fileObj = open('c:\\scrip ts\\file.txt')
                    Word = fileObj.read()
                    v = Word.count('Wor d')
                    fileObj.close()
                    if v > 2:
                    print "Word Here"
                    time.sleep(2)
                    temp = open ('c:\\scripts\\ file.txt','w')
                    temp.close()[/CODE]

                    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:[CODE=python]
                    >>> temp = open ('file.txt','w' )
                    >>> temp.close()
                    >>> [/CODE]Try:[CODE=python]# ...
                    time.sleep(2)
                    temp = open ('c:\\scripts\\ file.txt','w')
                    print temp[/CODE]It must look like
                    ('c:\\scripts\\ file.txt', 'w')
                    to make any sense at all.

                    Comment

                    • T00l
                      New Member
                      • Jul 2007
                      • 16

                      #11
                      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

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #12
                        Originally posted by T00l
                        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.

                        Comment

                        Working...