#!/usr/bin/python num=0; ni=open("C:\Python34\ceshi.txt") for line in ni: num=num+1; #表示行数 if num==4: #当num为4时,打印出第四行的内容,在开始的编写中写成了num=4,运行时一直提示语法错误,找了半天原因才明白需写成==,=是赋值,==是比较 print(line) ni.close() #import linecache #print(linecache.getline(
用类方法和静态方法实现:一个是追加写文件一行内容,一个是读指定行号的内容 #coding=utf-8 class handle_file(object): def __init__(self,file_path): self.file_path=file_path @classmethod def write_file(cls,file_path,content): with open(file_path,'a') as