Pythonベース(7)ファイル管理クラス

1826 ワード

Pythonファイル管理クラス
# -*- coding: UTF-8 -*-

import os

print "     "

class FileManagerClass:
	def __init__(self):
		return
	#    
	def OpenFile(self,filepath,openWay):
		print "****     "
		print "*******    : ",filepath,"     :",openWay
		fo = open(filepath,openWay)
		return fo;

	#   
	def ReadFile(self,fo,lenth):
		print "****   "
		str = fo.read(lenth)
		print "*******       :", str 
		return str;

	#   
	def  WriteFile(self,fo,str):
	   print "****   "
	   fo.write(str);
	   return;


	#    
	def CloseFile(self,fo):
	   print "****    "
	   fo.close()
	   return

	#   
	def RenameFile(self,file_name,newname):
	   print "****     ",file_name,"  : ",newname
	   os.rename(file_name,newname)
	   return

	#    
	def DeleteFile(self,file_name):
	   print "****     :",file_name
	   str = raw_input("        Y/N:")
	   if str == "Y" or str == "y":
		  os.remove(file_name)
		  print file_name,"      "
	   else:
		  print "       "
	   return;

	#          
	def WriteFileByInput(self,filename):
	   print "****          "
	   str = raw_input("     ");
	   print "        :",str
	   fo = self.OpenFile(filename,"a+")
	   self.WriteFile(fo,str)
	   self.CloseFile(fo)
	   return;


#  

myPath = "test.txt"
newPath = "newTxst.txt"
fileManager = FileManagerClass()


fo = fileManager.OpenFile(myPath ,"a+")
fileManager.WriteFile(fo,"I\'m writing string to this file !")
#               
position = fo.seek(0, 0);
stringInFile = fileManager.ReadFile(fo,120)
fileManager.CloseFile(fo)
fileManager.WriteFileByInput(myPath)
fileManager.RenameFile(myPath,newPath)
fileManager.DeleteFile(newPath)