枚举文件加入特定字符串


import os,os.path
path = os.path
class VersionOper(object):
'The version appendor class .'
__dir =''
__appendStr='mrjeye'
__isDebug =False
__doType =()

def debug(self,msg):
'prints the msg if debug mode .'
if not self.__isDebug:
return
print 'JDebug , msg :%s' %msg

def addType(self,item):
'add the oper type .'
self.__doType =self.__doType.__add__((item,))

def __init__(self,path,s='mrjeye',isDebug=False):
if not os.path.exists(path):
raise IOError('path %s is not exists !' %path)
self.__dir = path
self.__appendStr = s #the string what user want to append
self.__isDebug = isDebug

def append(self):
'begin to enum and try append data .'
self.enum(self.__dir)

def enum(self,_path):
'Enum files in _path .'
items = os.listdir(_path)
for item in items:
_npath = path.join(_path,item)
if path.isfile (_npath):
self.debug('enum a fiile :%s' % _npath)
if self.isNoDo(_npath):
continue
self.doAppend(_npath)
elif path.isdir (_npath):
self.debug('enum a dir :%s' % _npath)
self.enum(_npath)

def doAppend(self,f):
'append str to a file .'
try:
_file = file(f,'a');
#lines = _file.readlines()
#for line in lines:
_file.write(self.__appendStr)
_file.close()
self.debug('append to %s success !' %f)
except IOError , e:
self.debug( 'error to append file , msg :%s' %e)

def isNoDo(self,f):
'check the file .'
ft = self.getFileType(f)
self.debug(ft)
for item in self.__doType:
if ft == item:
return False
return True

def getFileType(self,f):
ret =''
for i in range(len(f)):
if f[i] =='.':
ret = f[i+1:]
return ret

if __name__ == '__main__':
vo = VersionOper('c:\\1','test append',True)
#append two file type to the oper object
vo.addType('txt')
vo.addType('log')

vo.append()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值