how can i read specific line

This is the place for queries that don't fit in any of the other categories.

how can i read specific line

Postby avivfl » Sun Apr 20, 2014 3:31 pm

Code: Select all
def openfile():
    f = open('/home/avivfl/scrips/aviv.txt','r')
    print f.read(2)


when i write print f.read(2), the file is printing the first two chracters of the line,
how can i read specific line, for example line 3 only?
avivfl
 
Posts: 8
Joined: Sat Mar 29, 2014 8:13 am

Re: how can i read specific line

Postby metulburr » Sun Apr 20, 2014 3:37 pm

f.read(2) is just reading the first two bytes of the file

how can i read specific line, for example line 3 only?

Code: Select all
lines = f.readlines()
print(lines[2])

which is going to put the entire file into a list indexed by newlines, and print out line 3
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests