zoukankan      html  css  js  c++  java
  • python 用类方法和静态方法实现是追加写文件内容,和读指定行号的内容

    用类方法和静态方法实现:一个是追加写文件一行内容,一个是读指定行号的内容

     

    #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 fp:

                fp.write(content)

       

        def readContent(self):

            with open(self.file_path) as fp:

                return fp.read()

     

        @staticmethod

        def read_file(file_path,line_nbr):

            with open(file_path,'r') as fp:

                strList=fp.readlines()

                print strList

                if strList[line_nbr-1]==' ':

                    print 'error'

                else:

                    return strList[line_nbr-1]

     

     

     

    s="1ssdfsdf 2dfsdfs 3sdfsdf "

     

    operation=handle_file('d:\0410.txt')

    handle_file.write_file('d:\0410.txt',s)

    print handle_file.read_file('d:\0410.txt',2)

    print operation.readContent()

     

    c:Python27Scripts>python task_test.py

    ['1ssdfsdf ', '2dfsdfs ', '3sdfsdf ']

    2dfsdfs

     

    1ssdfsdf

    2dfsdfs

    3sdfsdf

  • 相关阅读:
    Spring、实例化Bean的三种方法
    Spring、编码剖析Spring管理Bean的原理
    Spring、Hello Spring
    Spring、控制反转与依赖注入(概念)
    Hibernate、批量操作数据
    Hibernate、3.6.7在线帮助文档
    Hibernate、乐观锁和悲观锁
    JQuery UI 精品UI推荐
    java 为pdf添加水印图片
    Hibernate 、继承关联映射
  • 原文地址:https://www.cnblogs.com/xiaxiaoxu/p/8778210.html
Copyright © 2011-2022 走看看