当前位置:编程学习 > 网站相关 >>

python 读取文件的前几行

#-*- 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()

补充:Web开发 , Python ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,