Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links


How to read a file in reverse order in python?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 20-02-2010
Member
 
Join Date: Aug 2009
Posts: 59
How to read a file in reverse order in python?

Sponsored Links
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.

Reply With Quote
  #2  
Old 20-02-2010
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,943
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()
Reply With Quote
  #3  
Old 20-02-2010
Member
 
Join Date: May 2008
Posts: 2,000
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:
Code:
$ tac file
Reply With Quote
  #4  
Old 20-02-2010
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,991
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
Reply With Quote
  #5  
Old 20-02-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,278
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)
Reply With Quote
  #6  
Old 20-02-2010
absolute55's Avatar
Member
 
Join Date: Nov 2005
Posts: 1,321
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()
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to read a file in reverse order in python?"
Thread Thread Starter Forum Replies Last Post
Need help to reverse the name order in Microsoft Excel Kesar MS Office Support 3 24-01-2012 01:16 PM
How to Reverse Row Order in Microsoft Excel Raju Chacha MS Office Support 1 14-01-2012 05:25 PM
How to reverse Page Order in Acrobat Pro 9 Patricia-USA Windows Software 5 13-07-2010 06:38 AM
How can I reverse the order of text on a Excel cell Sunny55 Windows Software 3 06-06-2009 09:50 PM
How to reverse print order in Word 2007?? NavinS MS Office Support 1 22-01-2008 11:47 PM


All times are GMT +5.5. The time now is 04:22 PM.