xq8995209 发表于 2017-4-27 09:38:07

python 之文件追加

  文件的追加和读取:

#!/usr/bin/env python
import sys;
def WriteFile(strPath, strVal):
file = None;
bRet = False;
if not strPath or not strVal:
return False;
try:
file = open(strPath, "a+");
file.write(strVal);
file.flush();
bRet = True;
finally:
if file:
file.close();
file = None;
return bRet;
def ReadFile(strFile):
file = None;
bRet = False;
strVal = "";
if not strFile:
return ;
try:
file = open(strFile, "r");
strVal = file.read();
bRet = True;
finally:
if file:
file.close();
file = None;
return ;
if "__main__" == __name__:
strFile = "./a.log";
strWrite = "hello, andylin!\n";
if not WriteFile(strFile, strWrite):
print("write file failed!");
sys.exit(1);
lsRead = ReadFile(strFile);
if not lsRead:
print("read file failed!");
else:
print("read str:%s" %lsRead);
sys.exit(0);

 
页: [1]
查看完整版本: python 之文件追加