|
文件的追加和读取:
#!/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 [bRet, strVal];
try:
file = open(strFile, "r");
strVal = file.read();
bRet = True;
finally:
if file:
file.close();
file = None;
return [bRet, strVal];
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[0]:
print("read file failed!");
else:
print("read str:%s" %lsRead[1]);
sys.exit(0);
|
|
|