import os
class writeDoc:
#创建文件对象
def __init__(self,name):
self.name=name
try:
f=open(name,'w+')
self.f=f
except IOError,e:
print e
else:
print 'bulid file successfully'
#向文件中写入数据
def writedata(self,doc):
try:
self.f.write(doc)
except Exception,e:
print e
else:
print 'Write data successfully,find it here:%s' % os.path.abspath(self.name) #指定文件路径
#关闭文件
def closefile(self):
self.f.flush()
self.f.close()
p=writeDoc('G:\kg.txt')
p.writedata("Hello")
p.closefile()
版权声明:本文为博主原创文章,未经博主允许不得转载。