def safe_write(self, text):
import os.path
if not os.path.exists(self.filepath): # if the file does not exist, then can write
try:
with open(self.filepath, 'wt') as writer:
writer.write(text)
return True
except UnicodeEncodeError as ue:
self.write_utf8(text)
return True
except Exception as e:
if DEBUG_FLAG:
sys.stderr.write("Naked Framework Error: Unable to write to requested file with the safe_write() method (Naked.toolshed.file.py).")
raise e
else:
return False # if file exists, do not write and return False
#------------------------------------------------------------------------------
# [ safe_write_bin method ]
# Binary data file writer that will NOT overwrite existing file at the requested filepath
# returns boolean indicator for success of write based upon test for existence of file (False = write failed because file exists)
#------------------------------------------------------------------------------
评论列表
文章目录