Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
delete_file.py39 linesDownload Raw Back to utils
1import os
2import random
3import gc
4import time
5
6
7# Borrowed from https://github.com/rogerbinns/apsw/blob/master/apsw/tests.py#L224
8# Used to delete sqlite files on Windows, since Windows file locking
9# behaves differently to other operating systems
10# This should only be used for test or non-production code, such as in reset_state.
11def delete_file(name: str) -> None:
12    try:
13        os.remove(name)
14    except Exception:
15        pass
16
17    chars = list("abcdefghijklmn")
18    random.shuffle(chars)
19    newname = name + "-n-" + "".join(chars)
20    count = 0
21    while os.path.exists(name):
22        count += 1
23        try:
24            os.rename(name, newname)
25        except Exception:
26            if count > 30:
27                n = list("abcdefghijklmnopqrstuvwxyz")
28                random.shuffle(n)
29                final_name = "".join(n)
30                try:
31                    os.rename(
32                        name, "chroma-to-clean" + final_name + ".deletememanually"
33                    )
34                except Exception:
35                    pass
36                break
37            time.sleep(0.1)
38            gc.collect()
39 
codekingpro/portable-devtools · Team Ai