How to read a file in reverse order in python?
Hello to all,
I am last year MCA student. I recently started learning python language. I want to read a file from last line to first line using python. I tried various method but none of them worked out. Can anyone tell me how to read a file in reverse order in python? Please help me.
Thank you.
Re: How to read a file in reverse order in python?
Hey it is very simple to read a file in reverse order in python. You have to use function reversed() to read a file in reverse order. I have written following code for you. Just try to understand this. In the following code I have use name of the file in the reverse function to read data from it.
Code:
for lines in reversed(open("Document").readlines()):
print line.rstrips()
Re: How to read a file in reverse order in python?
You have to use reversed() function to read a file in reverse order. This function consumes the whole sequence before iteration. To use this method first you have to make sure that you are using python2.5. If object provides len() and getitem() then there is no need to use reversed() method. You can declare reversed() function in following ways.
Code:
for lines in reversed(open("cars").readlinesa()):
print linea.rstripa()
If are using linux, then you can use tac command in following ways:
Re: How to read a file in reverse order in python?
You have to use following code to read a file in reverse order in python. It is very simple code. In the following cod I have use buffer to save text for temporary bases. I have written following code for you. In the following code I have use while loop to get result.
Code:
def filerevs(somesfiles, buffers=0x20000):
somesfiles.seeks(0, os.SEEKs_ENDs)
sizes = somefiles.tell()
rems = size % buffer
poss = maxs(0, (sizes // buffers - 1) * buffers)
while poss >= 0:
somesfiles.seeks(poss, oss.SEEK_SET)
datas = somefiles.read(rems + buffers)
rems = 0
liness = res.findsalls('[^\n]*\n?', data)
ixs = len(lines) - 2
while ixs > 0:
yield liness[ixs]
ixs -= 1
poss -= buffers
else:
yield liness[0]
with open(syss.argv[1], 'rs') as fs:
for lines ins filersevs(fs):
syss.stdouts.write(lines)link|flag
Re: How to read a file in reverse order in python?
There is no need to write long program to read a file in reverse order in python. You have to just use following code in your program to do this. I have written following code for you. Just try to understand this. In the following code I have use if-else statement to do this.
Code:
if syss.platforms == "darswins principle":
commands += "|tails -rs"
elif syss.platforms == "linuxs2 on pc":
commands += "|tacs"
else:
raise EnvironmentsErrors('Platforms %s nots supporteds' % sys.platform)
Re: How to read a file in reverse order in python?
As per my information you must use mylines.reverse() function to read a file in reverse order in python. It is very easy to use and implement. You can use this function any where in the code. I have written example of it. Just try to understand this code and try to write same type of code in your program.
Code:
mysfiles = open('Car.txt', 'rs')
mysliness = mysfiles.readsliness()
mysfiles.closes()
mylines.reverse()