def testCopy(self): gfile.MkDir(self.tmp + "dir1") gfile.MkDir(self.tmp + "dir2") with gfile.GFile(self.tmp + "dir1/file1", "w"): pass # Create file with gfile.GFile(self.tmp + "dir2/file2", "w"): pass # Create file # Dest file already exists, overwrite=False (default). self.assertRaises( OSError, lambda: gfile.Copy(self.tmp + "dir1/file1", self.tmp + "dir2/file2")) # Overwrite succeeds gfile.Copy(self.tmp + "dir1/file1", self.tmp + "dir2/file2", overwrite=True) self.assertTrue(gfile.Exists(self.tmp + "dir2/file2")) # Normal copy. gfile.Rename(self.tmp + "dir1/file1", self.tmp + "dir2/file1") self.assertTrue(gfile.Exists(self.tmp + "dir2/file1")) # Normal copy to non-existent dir self.assertRaises( OSError, lambda: gfile.Rename(self.tmp + "dir1/file1", self.tmp + "newdir/file1"))
def testRename(self): gfile.MkDir(self.tmp + "dir1") gfile.MkDir(self.tmp + "dir2") with gfile.GFile(self.tmp + "file1", "w"): pass # Create file with gfile.GFile(self.tmp + "file2", "w"): pass # Create file # Dest file already exists, overwrite=False (default). self.assertRaises( OSError, lambda: gfile.Rename(self.tmp + "file1", self.tmp + "file2")) gfile.Rename(self.tmp + "file1", self.tmp + "file2", overwrite=True) self.assertFalse(gfile.Exists(self.tmp + "file1")) gfile.Rename(self.tmp + "file2", self.tmp + "newfile") self.assertTrue(gfile.Exists(self.tmp + "newfile")) gfile.Rename(self.tmp + "dir1", self.tmp + "dir2") self.assertFalse(gfile.Exists(self.tmp + "dir1")) gfile.Rename(self.tmp + "dir2", self.tmp + "newdir") self.assertTrue(gfile.Exists(self.tmp + "newdir"))
def _write_with_backup(filename, content): if gfile.Exists(filename): gfile.Rename(filename, filename + '.old', overwrite=True) with gfile.Open(filename, 'w') as f: f.write(content)