Python:Detect if the current line in file read is the last one
up vote
9
down vote
favorite
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
|
show 2 more comments
up vote
9
down vote
favorite
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
@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
|
show 2 more comments
up vote
9
down vote
favorite
up vote
9
down vote
favorite
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
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
python
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
|
show 2 more comments
@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
|
show 2 more comments
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
Probably the simplest way to do this.
– Doug
Jul 27 '14 at 16:43
add a comment |
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)
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 dosize -= 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
add a comment |
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
2
Your solution is bad because it'll load the whole file in the memory, and it'll justsegfault
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
add a comment |
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.
add a comment |
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
add a comment |
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
add a comment |
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
Probably the simplest way to do this.
– Doug
Jul 27 '14 at 16:43
add a comment |
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
Probably the simplest way to do this.
– Doug
Jul 27 '14 at 16:43
add a comment |
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
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
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
add a comment |
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
add a comment |
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)
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 dosize -= 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
add a comment |
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)
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 dosize -= 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
add a comment |
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)
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)
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 dosize -= 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
add a comment |
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 dosize -= 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
add a comment |
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
2
Your solution is bad because it'll load the whole file in the memory, and it'll justsegfault
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
add a comment |
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
2
Your solution is bad because it'll load the whole file in the memory, and it'll justsegfault
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
add a comment |
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
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
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 justsegfault
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
add a comment |
2
Your solution is bad because it'll load the whole file in the memory, and it'll justsegfault
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jul 27 '14 at 16:36
hoodakaushal
436918
436918
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jul 27 '14 at 16:49
inspectorG4dget
60.7k1694179
60.7k1694179
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 10 at 23:22
Popov Florino
11
11
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
@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