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]=='\n':
print 'error'
else:
return strList[line_nbr-1]
s="1ssdfsdf\n2dfsdfs\n3sdfsdf\n"
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:\Python27\Scripts>python task_test.py
['1ssdfsdf\n', '2dfsdfs\n', '3sdfsdf\n']
2dfsdfs
1ssdfsdf
2dfsdfs
3sdfsdf
python 用类方法和静态方法实现是追加写文件内容,和读指定行号的内容的更多相关文章
- shell脚本实现覆盖写文件和追加写文件
		
1.覆盖写文件 ">" date > not_append_file.txt
 - python 浅析类方法与静态方法
		
类方法,静态方法的定义 Python 是双面向的,既可以面向函数编程,也可以面向对象编程,所谓面向函数就是单独一个. py 文件,里面没有类,全是一些函数,调用的时候导入模块,通过模块名.函数名()即 ...
 - python 面向对象 类方法,静态方法,property
		
property 内置装饰器函数 只在面向对象使用 把方法当初属性使用(方法不加参数) 例子: class Rectangle: def __init__(self,long,wide,color): ...
 - python读取文件行号和内容的便捷方法
		
处理数据时候,需要得到数据所在和行号,使用enumerate时便捷的方法: file = open('file.txt','r') for (num,value) in enumerate(file) ...
 - Java以UTF-8格式读写及追加写文件示例
		
package test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...
 - Python: 读文件,写文件
		
读写文件是最常见的IO操作.Python内置了读写文件的函数. 读写文件前,我们先了解一下,在磁盘上读写文件的功能都是有操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求 ...
 - python类的实例方法\静态方法\类方法区别解析(附代码)
		
前言 搞了好久python,一直搞得不太清楚这几种类的方法,今天花时间好好测试一下,算是弄懂点皮毛吧. 三种方法的不同 先剽窃个图看一下 可以看到,实例是三种方法都可以调用的,而类只可以调用两种.所以 ...
 - python类方法和静态方法
		
C++的静态方法是用static关键字,python j是没用static的. python中实现静态方法和类方法都是依赖于python的修饰器来实现的. class MyClass: def me ...
 - 关于python的类方法、实例方法和静态方法区别
		
python的类方法需要在方法前面加装饰器:@classmethod ,静态方法是在方法前面加装饰器:@staticmethod. 类方法.类属性是属于类自身,属于类自身的命名空间,和实例方法.实例属 ...
 
随机推荐
- webp图片优化
			
根据对目前国内浏览器占比与 WebP 的兼容性分析,大约有 50% 以上的国内用户可以直接体验到 WebP,如果你的网站以图片为主,或者你的产品基于 Chromium 内核,建议体验尝试.假如你打算在 ...
 - Maven使用tomcat8-maven-plugin插件
			
在网上搜索一堆文章没找到解决方法,只找到了tomcat7-maven-plugin的plugin,如下: <plugin> <groupId>org.apache.tomcat ...
 - return 通过文件后缀名得到的函数字符串
			
<?php//图片处理工具类class Image{//属性private $thumb_width; //缩略图的宽private $thumb_height;//错误属性public $th ...
 - ubuntu16.04下安装kdevelop和汉化
			
1.Kdevelop安装 最简单的命令行安装,打开终端,执行 sudo apt-get install kdevelop 2.Kdevelop汉化 不需要自己下载汉化包,Kdevelop安装后,在终端 ...
 - PyQT5初学(一)
			
PyQt5 是Digia的一套Qt5与python绑定的应用框架,同时支持2.x和3.x.本教程使用的是3.x.Qt库由Riverbank Computing开发,是最强大的GUI库之一 ,官方网站: ...
 - Linux新建用户 useradd&groupadd
			
建立一个新组,并设置组ID加入系统:#groupadd -g 1000 sparkgroup #useradd -u 2000 -g sparkgroup sparkuser #mkdir -p /a ...
 - 【Python】关于Python多线程的一篇文章转载
			
猪哥推荐的学习网址 http://www.jb51.net/article/110164.htm yeayee ------>更多技巧------>更多源码------>http:/ ...
 - [py][mx]django自带后台系统使用
			
django的manytomany字段和后台搜索过滤功能 后台开发一般要求 后台要求能快速搭建, 主要精力放在前端用户系统开发上. 权限管理 少量样式 快速开发 django自带的后台手动注册模型 创 ...
 - Python爬虫第一个成功版
			
爬取http://www.mzitu.com/all里面的图片 import urllib.request import re import os url = 'http://www.mzitu.co ...
 - [LeetCode] 127. Word Ladder _Medium  tag: BFS
			
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...