read & print only first & last line of txt file

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 1,297
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 68
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

read & print only first & last line of txt file

 
0
  #1
Apr 29th, 2008
Hi all, hope you are well. Just a quick one here that you pythons will be able to answer in no time (I am new with python). What I am trying to do is read a text file and print only the first and last line of that file. Any ideas as to how I might go about this ? I have read the file so all i need to do is find a way of finding out which is first and last line, and then printing them. Thanks for your time.
Computers are man's attempt at designing a cat: It does whatever it wants, whenever it wants, and rarely ever at the right time.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,297
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 68
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: read & print only first & last line of txt file

 
0
  #2
Apr 29th, 2008
Ok I have now established how to print the last line of a text file. Anyone interested here is where I found the answer:

I still need to figure out how to print the first line of the text file, so any ideas on this are welcome!
Computers are man's attempt at designing a cat: It does whatever it wants, whenever it wants, and rarely ever at the right time.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,297
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 68
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: read & print only first & last line of txt file

 
0
  #3
Apr 29th, 2008
ok ignore the above link - it is a search link which doesnt seem to work. Here is the solution that I have worked out:

  1. def lab01():
  2. file = open('filename.txt','r')#specify file to open
  3. data = file.readlines() #read lines in file and put
  4. #into LIST called data
  5. print "Last line = "
  6. print data[len(data)-1] #-1 represents last item on list data
  7. print "First line = "
  8. file.seek ( 0 )#seek to first line in file
  9. print file.readline()
  10. file.close() #good practice to close files after use
Last edited by majestic0110; Apr 29th, 2008 at 12:09 pm.
Computers are man's attempt at designing a cat: It does whatever it wants, whenever it wants, and rarely ever at the right time.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,297
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 68
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: read & print only first & last line of txt file

 
0
  #4
Apr 29th, 2008
Restructured the code to make it a little more elegant now:

  1. def lab01():
  2. file = open('swift20080428172404.txt','r')#specify file to open
  3. data = file.readlines() #read lines in file and put into
  4. #LIST called data
  5. file.close() #good practice to close files after use
  6. print "Last line = "
  7. print data[len(data)-1] #-1 represents last item on list data
  8. print "First line = "
  9. print data[0] #-1 represents last item on list data
Last edited by majestic0110; Apr 29th, 2008 at 12:16 pm.
Computers are man's attempt at designing a cat: It does whatever it wants, whenever it wants, and rarely ever at the right time.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 4528 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC