python 读取文件的前几行
程序员文章站
2023-11-10 15:30:34
#-*- coding:utf-8 -
'''
created on 2012-8-20
@author : shaolei
''...
#-*- coding:utf-8 -
'''
created on 2012-8-20
@author : shaolei
'''
#输入数字n和文件名f ,显示文件f的前n行内容
#提示用户输入要读取文件前几行
n = int(raw_input('enter a line number : >> '))
filename = raw_input('enter a file path: >> ')
file = open(filename,'r')
linenum = 0
for line in file.readlines()[0:n]:
print line
file.close()