#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    40
    Rep Power
    11

    Question Logging or finding certain line in a text file


    I have a text file that is constantly being written to. Every so often the file will stop being written to and I need to read the file and take the data written since the last read and merge it with another file and work some Python magic. I want to write the last line of the the current file into a text file or give it a line number and write that to a text file so that next time i open it i can find the line or line number from the text file and start from the following line to the end of the file.

    here is a sample of the text file. Any help or pointers in the right direction would be greatly appreciated

    "22/04/2005 10:09:12","04610009323EDD94","true"
    "22/04/2005 10:09:13","04610009323EDD95","true"
    "22/04/2005 10:09:14","04610009323EDD96","true"
    "22/04/2005 10:09:16","04610009323EDD97","true"
    "22/04/2005 10:09:16","04610009323EDD98","true"
    "22/04/2005 10:09:18","","false"
    "22/04/2005 10:09:26","","false"
    "22/04/2005 10:09:27","04610009323EDD19","true"
    "22/04/2005 10:09:28","","false"
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Code:
    for line in file('your_file_name.txt'):
        if(line is 'the line you are looking for'):
            #do stuff

    Comments on this post

    • CyBerHigh agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    40
    Rep Power
    11
    thanks but i need to know how to read all the lines in the file into a list after the line that i am looking for
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    I thinm I understand what you want, let me clarify this. You want every line after the line you are looking for to be added into a list? Here is the code to do so:
    Code:
    data_list = []
    for line in file('your_file_name.txt'):
        if(line is 'the line you are looking for'):
            found_line = [True]
        else:
            found_line = [False]
        if(found_line is True):
            data_list.append(line)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    you could have that easier in the first "if":
    just do data_list.append(line)

    or there is another way you could do it:

    Code:
    >>> File = open("hello.txt","r")
    >>> List = File.readlines() 		# Makes a List of all the lines
    >>> other_List = []
    >>> for Line in List:
    	if Line is "Line your searching for":
    		other_List.append(Line)
    		break
    	else: pass
    Last edited by monkeyman23555; May 12th, 2005 at 03:50 PM.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by macca1707
    thanks but i need to know how to read all the lines in the file into a list after the line that i am looking for
    You should be able to do something lik this with the readlines() method and a simple slice. Something like this:

    Code:
    >>> lineNumber = 50
    >>> allLines = file('sample.txt', 'r').readlines()
    >>> newLines = allLines[lineNumber:]
    ...
    >>>
    Very simply, this reads the file into a list and assigns all the lines after 50 to a new variable.

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by †Yegg†
    I thinm I understand what you want, let me clarify this. You want every line after the line you are looking for to be added into a list? Here is the code to do so:
    Code:
    data_list = []
    for line in file('your_file_name.txt'):
        if(line is 'the line you are looking for'):
            found_line = [True]
        else:
            found_line = [False]
        if(found_line is True):
            data_list.append(line)
    Both of these examples could be cut down to something like:

    Code:
    lines = []
    
    for line in file('sample.txt'):
        if(line == 'the line'): lines.append(line)
    or...

    Code:
    lines = [line for line in file('sample.txt') if line == 'the line']
    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  14. #8
  15. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    I realized that, but I wasn't thinking about code length, I was just posting code that did what he wanted. Not that yours doesn't, yours was just coded better.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by macca1707
    I have a text file that is constantly being written to. Every so often the file will stop being written to and I need to read the file and take the data written since the last read
    You need to be using tell() to find out current positions and seek() to go there:

    Code:
    from time import time, sleep
    
    try:
        try:
            myfile = open("C:/foo.txt", "r+a")
        except IOError:
            open("C:/foo.txt", "w").close()
            myfile = open("C:/foo.txt", "r+a")
    
        for i in range(5):
            myfile.seek(0L, 2)
            currpos = myfile.tell()
            print "----- %s"%i
            for j in range(4):
                sleep(.001)
                print>>myfile, "=> %s"%time()
            myfile.seek(currpos, 0)
            for line in myfile.readlines():
                print line[:-1]
    
    finally:
        myfile.close()
    --OH.

IMN logo majestic logo threadwatch logo seochat tools logo