| | |
read & print only first & last line of txt file
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
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!
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.
ok ignore the above link - it is a search link which doesnt seem to work. Here is the solution that I have worked out:
python Syntax (Toggle Plain Text)
def lab01(): file = open('filename.txt','r')#specify file to open data = file.readlines() #read lines in file and put #into LIST called data print "Last line = " print data[len(data)-1] #-1 represents last item on list data print "First line = " file.seek ( 0 )#seek to first line in file print file.readline() 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.
Restructured the code to make it a little more elegant now:
python Syntax (Toggle Plain Text)
def lab01(): file = open('swift20080428172404.txt','r')#specify file to open data = file.readlines() #read lines in file and put into #LIST called data file.close() #good practice to close files after use print "Last line = " print data[len(data)-1] #-1 represents last item on list data print "First line = " 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.
![]() |
Similar Threads
- Function & writing to file problem (Python)
Other Threads in the Python Forum
- Previous Thread: Function & writing to file problem
- Next Thread: Text to Speech using COM (Python)
Views: 4528 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt apache approximation argv array beginner binary book calculator change cipher clear code compile converter countpasswordentry cturtle dictionaries dictionary drive dynamic examples excel file float format ftp function gui homework import inches input java keyboard library line linux list lists loop maze mouse mysqldb mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive remote scrolledtext search session signal singleton socket ssh string strings strip table terminal text textarea thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable verify vigenere windows wordgame wxpython xlwt






