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()
枚举文件加入特定字符串

最新推荐文章于 2024-12-18 19:19:41 发布