コード例 #1
0
ファイル: test_utils.py プロジェクト: Web5design/Bento
 def _test_rename(self):
     d = tempfile.mkdtemp()
     try:
         f = op.join(d, "f.txt")
         fid = open(f, "wt")
         try:
             fid.write("")
         finally:
             fid.close()
         g = op.join(d, "g.txt")
         rename(f, g)
         self.assertTrue(op.exists(g))
     finally:
         shutil.rmtree(d)
コード例 #2
0
 def _test_rename(self):
     d = tempfile.mkdtemp()
     try:
         f = op.join(d, "f.txt")
         fid = open(f, "wt")
         try:
             fid.write("")
         finally:
             fid.close()
         g = op.join(d, "g.txt")
         rename(f, g)
         self.assertTrue(op.exists(g))
     finally:
         shutil.rmtree(d)
コード例 #3
0
def safe_write(target, writer, mode="wb"):
    """a 'safe' way to write to files.

    Instead of writing directly into a file, this function writes to a
    temporary file, and then rename the file to the target. On sane
    platforms, rename is atomic, so this avoids leaving stale
    files in inconsistent states.

    Parameters
    ----------
    target: str
        destination to write to
    writer: callable
        function which takes one argument, a file descriptor, and
        writes content to it
    mode: str
        opening mode
    """
    f = open(target + ".tmp", mode)
    try:
        writer(f)
    finally:
        f.close()
        rename(target + ".tmp", target)
コード例 #4
0
ファイル: io2.py プロジェクト: B-Rich/Bento
def safe_write(target, writer, mode="wb"):
    """a 'safe' way to write to files.

    Instead of writing directly into a file, this function writes to a
    temporary file, and then rename the file to the target. On sane
    platforms, rename is atomic, so this avoids leaving stale
    files in inconsistent states.

    Parameters
    ----------
    target: str
        destination to write to
    writer: callable
        function which takes one argument, a file descriptor, and
        writes content to it
    mode: str
        opening mode
    """
    f = open(target + ".tmp", mode)
    try:
        writer(f)
    finally:
        f.close()
        rename(target + ".tmp", target)