Python:Detect if the current line in file read is the last one











up vote
9
down vote

favorite
3












I am reading a file in Python line by line and I need to know which line is the last one while reading,something like this:



 f = open("myfile.txt")
for line in f:
if line is lastline:
#do smth


From the examples I found it involves seeks and complete file readouts to count lines,etc.Can I just detect that the current line is the last one? I tried to go and check for "n" existence ,but in many cases the last lines is not followed by backslash N.



Sorry if my question is redundant as I didn't find the answer on SO










share|improve this question
























  • @vaultah that's a nice trick but I need to know at the last line that it is the last :)
    – Michael IV
    Jul 27 '14 at 16:33










  • so you are only interested in the last line?
    – Padraic Cunningham
    Jul 27 '14 at 16:36










  • What are you going to do when you detect it's the last line? Could you just move whatever that is outside of the for loop?
    – dano
    Jul 27 '14 at 16:37










  • I use all the lines but I need the last one to append some different stuff before I write it into another file at the same place.I mean,yeah,I can just do it in else: after "for in " but it is really messy..
    – Michael IV
    Jul 27 '14 at 16:42










  • // is not the python syntax for commenting. Are you sure you're looking for an answer in python?
    – inspectorG4dget
    Jul 27 '14 at 16:42















up vote
9
down vote

favorite
3












I am reading a file in Python line by line and I need to know which line is the last one while reading,something like this:



 f = open("myfile.txt")
for line in f:
if line is lastline:
#do smth


From the examples I found it involves seeks and complete file readouts to count lines,etc.Can I just detect that the current line is the last one? I tried to go and check for "n" existence ,but in many cases the last lines is not followed by backslash N.



Sorry if my question is redundant as I didn't find the answer on SO










share|improve this question
























  • @vaultah that's a nice trick but I need to know at the last line that it is the last :)
    – Michael IV
    Jul 27 '14 at 16:33










  • so you are only interested in the last line?
    – Padraic Cunningham
    Jul 27 '14 at 16:36










  • What are you going to do when you detect it's the last line? Could you just move whatever that is outside of the for loop?
    – dano
    Jul 27 '14 at 16:37










  • I use all the lines but I need the last one to append some different stuff before I write it into another file at the same place.I mean,yeah,I can just do it in else: after "for in " but it is really messy..
    – Michael IV
    Jul 27 '14 at 16:42










  • // is not the python syntax for commenting. Are you sure you're looking for an answer in python?
    – inspectorG4dget
    Jul 27 '14 at 16:42













up vote
9
down vote

favorite
3









up vote
9
down vote

favorite
3






3





I am reading a file in Python line by line and I need to know which line is the last one while reading,something like this:



 f = open("myfile.txt")
for line in f:
if line is lastline:
#do smth


From the examples I found it involves seeks and complete file readouts to count lines,etc.Can I just detect that the current line is the last one? I tried to go and check for "n" existence ,but in many cases the last lines is not followed by backslash N.



Sorry if my question is redundant as I didn't find the answer on SO










share|improve this question















I am reading a file in Python line by line and I need to know which line is the last one while reading,something like this:



 f = open("myfile.txt")
for line in f:
if line is lastline:
#do smth


From the examples I found it involves seeks and complete file readouts to count lines,etc.Can I just detect that the current line is the last one? I tried to go and check for "n" existence ,but in many cases the last lines is not followed by backslash N.



Sorry if my question is redundant as I didn't find the answer on SO







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 12:17









Community

11




11










asked Jul 27 '14 at 16:28









Michael IV

5,102860151




5,102860151












  • @vaultah that's a nice trick but I need to know at the last line that it is the last :)
    – Michael IV
    Jul 27 '14 at 16:33










  • so you are only interested in the last line?
    – Padraic Cunningham
    Jul 27 '14 at 16:36










  • What are you going to do when you detect it's the last line? Could you just move whatever that is outside of the for loop?
    – dano
    Jul 27 '14 at 16:37










  • I use all the lines but I need the last one to append some different stuff before I write it into another file at the same place.I mean,yeah,I can just do it in else: after "for in " but it is really messy..
    – Michael IV
    Jul 27 '14 at 16:42










  • // is not the python syntax for commenting. Are you sure you're looking for an answer in python?
    – inspectorG4dget
    Jul 27 '14 at 16:42


















  • @vaultah that's a nice trick but I need to know at the last line that it is the last :)
    – Michael IV
    Jul 27 '14 at 16:33










  • so you are only interested in the last line?
    – Padraic Cunningham
    Jul 27 '14 at 16:36










  • What are you going to do when you detect it's the last line? Could you just move whatever that is outside of the for loop?
    – dano
    Jul 27 '14 at 16:37










  • I use all the lines but I need the last one to append some different stuff before I write it into another file at the same place.I mean,yeah,I can just do it in else: after "for in " but it is really messy..
    – Michael IV
    Jul 27 '14 at 16:42










  • // is not the python syntax for commenting. Are you sure you're looking for an answer in python?
    – inspectorG4dget
    Jul 27 '14 at 16:42
















@vaultah that's a nice trick but I need to know at the last line that it is the last :)
– Michael IV
Jul 27 '14 at 16:33




@vaultah that's a nice trick but I need to know at the last line that it is the last :)
– Michael IV
Jul 27 '14 at 16:33












so you are only interested in the last line?
– Padraic Cunningham
Jul 27 '14 at 16:36




so you are only interested in the last line?
– Padraic Cunningham
Jul 27 '14 at 16:36












What are you going to do when you detect it's the last line? Could you just move whatever that is outside of the for loop?
– dano
Jul 27 '14 at 16:37




What are you going to do when you detect it's the last line? Could you just move whatever that is outside of the for loop?
– dano
Jul 27 '14 at 16:37












I use all the lines but I need the last one to append some different stuff before I write it into another file at the same place.I mean,yeah,I can just do it in else: after "for in " but it is really messy..
– Michael IV
Jul 27 '14 at 16:42




I use all the lines but I need the last one to append some different stuff before I write it into another file at the same place.I mean,yeah,I can just do it in else: after "for in " but it is really messy..
– Michael IV
Jul 27 '14 at 16:42












// is not the python syntax for commenting. Are you sure you're looking for an answer in python?
– inspectorG4dget
Jul 27 '14 at 16:42




// is not the python syntax for commenting. Are you sure you're looking for an answer in python?
– inspectorG4dget
Jul 27 '14 at 16:42












6 Answers
6






active

oldest

votes

















up vote
3
down vote



accepted










secondLastLine = None
lastLine = None
with open("myfile.txt") as infile:
secondLastLine, lastLine = infile.readline(), infile.readline()
for line in infile:
# do stuff
secondLastLine = lastLine
lastLine = line

# do stuff with secondLastLine





share|improve this answer





















  • Probably the simplest way to do this.
    – Doug
    Jul 27 '14 at 16:43


















up vote
7
down vote













import os
path = 'myfile.txt'
size = os.path.getsize(path)
with open(path) as f:
for line in f:
size -= len(line)
if not size:
print('this is the last line')
print(line)





share|improve this answer























  • Does this handle CR+LF files correctly?
    – Thorbjørn Ravn Andersen
    Dec 22 '17 at 0:27










  • @ThorbjørnRavnAndersen Good point. It does not. You would need to do size -= len(line) + 1 instead. Here is an answer on how to identify the type of newline character: stackoverflow.com/a/2800981/778533
    – tommy.carstensen
    Dec 22 '17 at 0:50


















up vote
5
down vote













Check if line is the last line:



with open("in.txt") as f:
lines = f.readlines()
last = lines[-1]
for line in lines:
if line is last:
print id(line),id(last)
# do work on lst line
else:
# work on other lines


If you want the second last line use last = lines[-2]



Or simply:



with open("in.txt") as f:
lines = f.readlines()
last = lines[-1]
for line in lines[:-1]:
# work on all but last line
# work on last





share|improve this answer



















  • 2




    Your solution is bad because it'll load the whole file in the memory, and it'll just segfault for big enough files.
    – Denis Malinovsky
    Jul 21 '15 at 21:45










  • @DenisMalinovsky,where did the OP say that the file was large?
    – Padraic Cunningham
    Jul 21 '15 at 21:46






  • 1




    You're right, OP said "I am reading a file in Python line by line", not every line at once.
    – Denis Malinovsky
    Jul 21 '15 at 21:47


















up vote
2
down vote













One thing you could try is to try to get the next line, and catch the exception if it arises, because AFAIK python iterators don't have inbuilt hasNext method.






share|improve this answer




























    up vote
    2
    down vote













    You could use the itertools pairwise recipe;



    with open('myfile.txt') as infile:
    a,b = itertools.tee(infile)
    next(b, None)
    pairs = zip(a,b)
    lastPair = None
    for lastPair in pairs:
    pass
    secondLastLine = lastPair[0]
    # do stuff with secondLastLine





    share|improve this answer




























      up vote
      0
      down vote













      It's an old question, but if you want to allow empty last lines, this is better:



      with open("myfile.txt") as f:
      while True:
      line = f.readline()
      # do smth
      if line[-1:] != 'n':
      # do smth with the last line
      break


      or more readable (but a little bit slower):



      with open("myfile.txt") as f:
      while True:
      line = f.readline()
      # do smth
      if not line.endswith('n'):
      # do smth with the last line
      break





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24982993%2fpythondetect-if-the-current-line-in-file-read-is-the-last-one%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        down vote



        accepted










        secondLastLine = None
        lastLine = None
        with open("myfile.txt") as infile:
        secondLastLine, lastLine = infile.readline(), infile.readline()
        for line in infile:
        # do stuff
        secondLastLine = lastLine
        lastLine = line

        # do stuff with secondLastLine





        share|improve this answer





















        • Probably the simplest way to do this.
          – Doug
          Jul 27 '14 at 16:43















        up vote
        3
        down vote



        accepted










        secondLastLine = None
        lastLine = None
        with open("myfile.txt") as infile:
        secondLastLine, lastLine = infile.readline(), infile.readline()
        for line in infile:
        # do stuff
        secondLastLine = lastLine
        lastLine = line

        # do stuff with secondLastLine





        share|improve this answer





















        • Probably the simplest way to do this.
          – Doug
          Jul 27 '14 at 16:43













        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        secondLastLine = None
        lastLine = None
        with open("myfile.txt") as infile:
        secondLastLine, lastLine = infile.readline(), infile.readline()
        for line in infile:
        # do stuff
        secondLastLine = lastLine
        lastLine = line

        # do stuff with secondLastLine





        share|improve this answer












        secondLastLine = None
        lastLine = None
        with open("myfile.txt") as infile:
        secondLastLine, lastLine = infile.readline(), infile.readline()
        for line in infile:
        # do stuff
        secondLastLine = lastLine
        lastLine = line

        # do stuff with secondLastLine






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 27 '14 at 16:41









        inspectorG4dget

        60.7k1694179




        60.7k1694179












        • Probably the simplest way to do this.
          – Doug
          Jul 27 '14 at 16:43


















        • Probably the simplest way to do this.
          – Doug
          Jul 27 '14 at 16:43
















        Probably the simplest way to do this.
        – Doug
        Jul 27 '14 at 16:43




        Probably the simplest way to do this.
        – Doug
        Jul 27 '14 at 16:43












        up vote
        7
        down vote













        import os
        path = 'myfile.txt'
        size = os.path.getsize(path)
        with open(path) as f:
        for line in f:
        size -= len(line)
        if not size:
        print('this is the last line')
        print(line)





        share|improve this answer























        • Does this handle CR+LF files correctly?
          – Thorbjørn Ravn Andersen
          Dec 22 '17 at 0:27










        • @ThorbjørnRavnAndersen Good point. It does not. You would need to do size -= len(line) + 1 instead. Here is an answer on how to identify the type of newline character: stackoverflow.com/a/2800981/778533
          – tommy.carstensen
          Dec 22 '17 at 0:50















        up vote
        7
        down vote













        import os
        path = 'myfile.txt'
        size = os.path.getsize(path)
        with open(path) as f:
        for line in f:
        size -= len(line)
        if not size:
        print('this is the last line')
        print(line)





        share|improve this answer























        • Does this handle CR+LF files correctly?
          – Thorbjørn Ravn Andersen
          Dec 22 '17 at 0:27










        • @ThorbjørnRavnAndersen Good point. It does not. You would need to do size -= len(line) + 1 instead. Here is an answer on how to identify the type of newline character: stackoverflow.com/a/2800981/778533
          – tommy.carstensen
          Dec 22 '17 at 0:50













        up vote
        7
        down vote










        up vote
        7
        down vote









        import os
        path = 'myfile.txt'
        size = os.path.getsize(path)
        with open(path) as f:
        for line in f:
        size -= len(line)
        if not size:
        print('this is the last line')
        print(line)





        share|improve this answer














        import os
        path = 'myfile.txt'
        size = os.path.getsize(path)
        with open(path) as f:
        for line in f:
        size -= len(line)
        if not size:
        print('this is the last line')
        print(line)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 28 '15 at 22:34

























        answered Jan 28 '15 at 21:45









        tommy.carstensen

        3,61253266




        3,61253266












        • Does this handle CR+LF files correctly?
          – Thorbjørn Ravn Andersen
          Dec 22 '17 at 0:27










        • @ThorbjørnRavnAndersen Good point. It does not. You would need to do size -= len(line) + 1 instead. Here is an answer on how to identify the type of newline character: stackoverflow.com/a/2800981/778533
          – tommy.carstensen
          Dec 22 '17 at 0:50


















        • Does this handle CR+LF files correctly?
          – Thorbjørn Ravn Andersen
          Dec 22 '17 at 0:27










        • @ThorbjørnRavnAndersen Good point. It does not. You would need to do size -= len(line) + 1 instead. Here is an answer on how to identify the type of newline character: stackoverflow.com/a/2800981/778533
          – tommy.carstensen
          Dec 22 '17 at 0:50
















        Does this handle CR+LF files correctly?
        – Thorbjørn Ravn Andersen
        Dec 22 '17 at 0:27




        Does this handle CR+LF files correctly?
        – Thorbjørn Ravn Andersen
        Dec 22 '17 at 0:27












        @ThorbjørnRavnAndersen Good point. It does not. You would need to do size -= len(line) + 1 instead. Here is an answer on how to identify the type of newline character: stackoverflow.com/a/2800981/778533
        – tommy.carstensen
        Dec 22 '17 at 0:50




        @ThorbjørnRavnAndersen Good point. It does not. You would need to do size -= len(line) + 1 instead. Here is an answer on how to identify the type of newline character: stackoverflow.com/a/2800981/778533
        – tommy.carstensen
        Dec 22 '17 at 0:50










        up vote
        5
        down vote













        Check if line is the last line:



        with open("in.txt") as f:
        lines = f.readlines()
        last = lines[-1]
        for line in lines:
        if line is last:
        print id(line),id(last)
        # do work on lst line
        else:
        # work on other lines


        If you want the second last line use last = lines[-2]



        Or simply:



        with open("in.txt") as f:
        lines = f.readlines()
        last = lines[-1]
        for line in lines[:-1]:
        # work on all but last line
        # work on last





        share|improve this answer



















        • 2




          Your solution is bad because it'll load the whole file in the memory, and it'll just segfault for big enough files.
          – Denis Malinovsky
          Jul 21 '15 at 21:45










        • @DenisMalinovsky,where did the OP say that the file was large?
          – Padraic Cunningham
          Jul 21 '15 at 21:46






        • 1




          You're right, OP said "I am reading a file in Python line by line", not every line at once.
          – Denis Malinovsky
          Jul 21 '15 at 21:47















        up vote
        5
        down vote













        Check if line is the last line:



        with open("in.txt") as f:
        lines = f.readlines()
        last = lines[-1]
        for line in lines:
        if line is last:
        print id(line),id(last)
        # do work on lst line
        else:
        # work on other lines


        If you want the second last line use last = lines[-2]



        Or simply:



        with open("in.txt") as f:
        lines = f.readlines()
        last = lines[-1]
        for line in lines[:-1]:
        # work on all but last line
        # work on last





        share|improve this answer



















        • 2




          Your solution is bad because it'll load the whole file in the memory, and it'll just segfault for big enough files.
          – Denis Malinovsky
          Jul 21 '15 at 21:45










        • @DenisMalinovsky,where did the OP say that the file was large?
          – Padraic Cunningham
          Jul 21 '15 at 21:46






        • 1




          You're right, OP said "I am reading a file in Python line by line", not every line at once.
          – Denis Malinovsky
          Jul 21 '15 at 21:47













        up vote
        5
        down vote










        up vote
        5
        down vote









        Check if line is the last line:



        with open("in.txt") as f:
        lines = f.readlines()
        last = lines[-1]
        for line in lines:
        if line is last:
        print id(line),id(last)
        # do work on lst line
        else:
        # work on other lines


        If you want the second last line use last = lines[-2]



        Or simply:



        with open("in.txt") as f:
        lines = f.readlines()
        last = lines[-1]
        for line in lines[:-1]:
        # work on all but last line
        # work on last





        share|improve this answer














        Check if line is the last line:



        with open("in.txt") as f:
        lines = f.readlines()
        last = lines[-1]
        for line in lines:
        if line is last:
        print id(line),id(last)
        # do work on lst line
        else:
        # work on other lines


        If you want the second last line use last = lines[-2]



        Or simply:



        with open("in.txt") as f:
        lines = f.readlines()
        last = lines[-1]
        for line in lines[:-1]:
        # work on all but last line
        # work on last






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jul 27 '14 at 18:42

























        answered Jul 27 '14 at 17:04









        Padraic Cunningham

        132k12113187




        132k12113187








        • 2




          Your solution is bad because it'll load the whole file in the memory, and it'll just segfault for big enough files.
          – Denis Malinovsky
          Jul 21 '15 at 21:45










        • @DenisMalinovsky,where did the OP say that the file was large?
          – Padraic Cunningham
          Jul 21 '15 at 21:46






        • 1




          You're right, OP said "I am reading a file in Python line by line", not every line at once.
          – Denis Malinovsky
          Jul 21 '15 at 21:47














        • 2




          Your solution is bad because it'll load the whole file in the memory, and it'll just segfault for big enough files.
          – Denis Malinovsky
          Jul 21 '15 at 21:45










        • @DenisMalinovsky,where did the OP say that the file was large?
          – Padraic Cunningham
          Jul 21 '15 at 21:46






        • 1




          You're right, OP said "I am reading a file in Python line by line", not every line at once.
          – Denis Malinovsky
          Jul 21 '15 at 21:47








        2




        2




        Your solution is bad because it'll load the whole file in the memory, and it'll just segfault for big enough files.
        – Denis Malinovsky
        Jul 21 '15 at 21:45




        Your solution is bad because it'll load the whole file in the memory, and it'll just segfault for big enough files.
        – Denis Malinovsky
        Jul 21 '15 at 21:45












        @DenisMalinovsky,where did the OP say that the file was large?
        – Padraic Cunningham
        Jul 21 '15 at 21:46




        @DenisMalinovsky,where did the OP say that the file was large?
        – Padraic Cunningham
        Jul 21 '15 at 21:46




        1




        1




        You're right, OP said "I am reading a file in Python line by line", not every line at once.
        – Denis Malinovsky
        Jul 21 '15 at 21:47




        You're right, OP said "I am reading a file in Python line by line", not every line at once.
        – Denis Malinovsky
        Jul 21 '15 at 21:47










        up vote
        2
        down vote













        One thing you could try is to try to get the next line, and catch the exception if it arises, because AFAIK python iterators don't have inbuilt hasNext method.






        share|improve this answer

























          up vote
          2
          down vote













          One thing you could try is to try to get the next line, and catch the exception if it arises, because AFAIK python iterators don't have inbuilt hasNext method.






          share|improve this answer























            up vote
            2
            down vote










            up vote
            2
            down vote









            One thing you could try is to try to get the next line, and catch the exception if it arises, because AFAIK python iterators don't have inbuilt hasNext method.






            share|improve this answer












            One thing you could try is to try to get the next line, and catch the exception if it arises, because AFAIK python iterators don't have inbuilt hasNext method.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 27 '14 at 16:36









            hoodakaushal

            436918




            436918






















                up vote
                2
                down vote













                You could use the itertools pairwise recipe;



                with open('myfile.txt') as infile:
                a,b = itertools.tee(infile)
                next(b, None)
                pairs = zip(a,b)
                lastPair = None
                for lastPair in pairs:
                pass
                secondLastLine = lastPair[0]
                # do stuff with secondLastLine





                share|improve this answer

























                  up vote
                  2
                  down vote













                  You could use the itertools pairwise recipe;



                  with open('myfile.txt') as infile:
                  a,b = itertools.tee(infile)
                  next(b, None)
                  pairs = zip(a,b)
                  lastPair = None
                  for lastPair in pairs:
                  pass
                  secondLastLine = lastPair[0]
                  # do stuff with secondLastLine





                  share|improve this answer























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    You could use the itertools pairwise recipe;



                    with open('myfile.txt') as infile:
                    a,b = itertools.tee(infile)
                    next(b, None)
                    pairs = zip(a,b)
                    lastPair = None
                    for lastPair in pairs:
                    pass
                    secondLastLine = lastPair[0]
                    # do stuff with secondLastLine





                    share|improve this answer












                    You could use the itertools pairwise recipe;



                    with open('myfile.txt') as infile:
                    a,b = itertools.tee(infile)
                    next(b, None)
                    pairs = zip(a,b)
                    lastPair = None
                    for lastPair in pairs:
                    pass
                    secondLastLine = lastPair[0]
                    # do stuff with secondLastLine






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 27 '14 at 16:49









                    inspectorG4dget

                    60.7k1694179




                    60.7k1694179






















                        up vote
                        0
                        down vote













                        It's an old question, but if you want to allow empty last lines, this is better:



                        with open("myfile.txt") as f:
                        while True:
                        line = f.readline()
                        # do smth
                        if line[-1:] != 'n':
                        # do smth with the last line
                        break


                        or more readable (but a little bit slower):



                        with open("myfile.txt") as f:
                        while True:
                        line = f.readline()
                        # do smth
                        if not line.endswith('n'):
                        # do smth with the last line
                        break





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          It's an old question, but if you want to allow empty last lines, this is better:



                          with open("myfile.txt") as f:
                          while True:
                          line = f.readline()
                          # do smth
                          if line[-1:] != 'n':
                          # do smth with the last line
                          break


                          or more readable (but a little bit slower):



                          with open("myfile.txt") as f:
                          while True:
                          line = f.readline()
                          # do smth
                          if not line.endswith('n'):
                          # do smth with the last line
                          break





                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            It's an old question, but if you want to allow empty last lines, this is better:



                            with open("myfile.txt") as f:
                            while True:
                            line = f.readline()
                            # do smth
                            if line[-1:] != 'n':
                            # do smth with the last line
                            break


                            or more readable (but a little bit slower):



                            with open("myfile.txt") as f:
                            while True:
                            line = f.readline()
                            # do smth
                            if not line.endswith('n'):
                            # do smth with the last line
                            break





                            share|improve this answer












                            It's an old question, but if you want to allow empty last lines, this is better:



                            with open("myfile.txt") as f:
                            while True:
                            line = f.readline()
                            # do smth
                            if line[-1:] != 'n':
                            # do smth with the last line
                            break


                            or more readable (but a little bit slower):



                            with open("myfile.txt") as f:
                            while True:
                            line = f.readline()
                            # do smth
                            if not line.endswith('n'):
                            # do smth with the last line
                            break






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 10 at 23:22









                            Popov Florino

                            11




                            11






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24982993%2fpythondetect-if-the-current-line-in-file-read-is-the-last-one%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Bressuire

                                Vorschmack

                                Quarantine