def test_temp_file_removes(self): foo = NamedTemporaryFile(suffix='foo.txt', delete=True) full_path = foo.temporary_file_path() path, name = os.path.split(full_path) self.assertRegexpMatches(name, r'\w+foo.txt') foo.close() self.assertFalse(os.path.isfile(full_path))
def test_temp_file_removes_manually(self): foo = NamedTemporaryFile(suffix='foo.txt', delete=False) full_path = foo.temporary_file_path() os.remove(full_path) self.assertFalse(os.path.isfile(full_path)) # although removed, shouldn't raise an error foo.close()
def get_output_file(self, in_file, instance, field, **kwargs): """Creates a temporary file. With regular `FileSystemStorage` it does not need to be deleted, instaed file is safely moved over. With other cloud based storage it is a good idea to set `delete=True`.""" return NamedTemporaryFile( mode='rb', suffix='_%s_%s%s' % (get_model_name(instance), field.name, self.get_ext()), delete=False)
def test_temp_file_keeps(self): foo = NamedTemporaryFile(suffix='foo.txt', delete=False) full_path = foo.temporary_file_path() foo.close() self.assertTrue(os.path.isfile(full_path))