Ejemplo n.º 1
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)
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 4
0
Archivo: io2.py Proyecto: 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)