def open_fsynced(file_name, mode="w", buffering=-1): """Context manager that opens `file_name` and will `fsync` it before closing. """ with open(file_name, mode, buffering) as file: try: yield file finally: file.flush() sos.fsync(file.fileno())
def open_fsynced (file_name, mode = "w", buffering = -1) : """Context manager that opens `file_name` and will `fsync` it before closing. """ with open (file_name, mode, buffering) as file : try : yield file finally : file.flush () sos.fsync (file.fileno ())
def open_to_replace(file_name, mode="w", buffering=-1, backup_name=None): """Context manager that opens a file with a temporary name and renames it to `file_name` after syncing and closing. If `backup_name` is specified, the old file is renamed to `backup_name`. """ dir, name = sos.path.split(file_name) with open_tempfile (mode, buffering, prefix = name, dir = dir) as \ (file, temp_name) : yield file file.flush() sos.fsync(file.fileno()) file.close() if backup_name: try: sos.rename(file_name, backup_name) except sos.error as exc: if exc.args[0] != errno.ENOENT: import traceback traceback.print_exc() sos.rename(temp_name, file_name)
def open_to_replace (file_name, mode = "w", buffering = -1, backup_name = None) : """Context manager that opens a file with a temporary name and renames it to `file_name` after syncing and closing. If `backup_name` is specified, the old file is renamed to `backup_name`. """ dir, name = sos.path.split (file_name) with open_tempfile (mode, buffering, prefix = name, dir = dir) as \ (file, temp_name) : yield file file.flush () sos.fsync (file.fileno ()) file.close () if backup_name : try : sos.rename (file_name, backup_name) except sos.error as exc : if exc.args [0] != errno.ENOENT : import traceback traceback.print_exc () sos.rename (temp_name, file_name)