Если я запускаю приведенный ниже скрипт, он выдает ошибку, показанную. Однако, если я переключаю терминал и запускаю ту же команду для удаления файла (os.remove("test.db")
), файл удаляется.
import gc
import os
import time
from sqlite3 import connect
from contextlib import contextmanager
file = "test.db"
@contextmanager
def temptable(cur: object):
cur.execute("create table points(x, int, y int)")
try:
yield
finally:
cur.execute("drop table points")
with connect(file) as conn:
cur = conn.cursor()
with temptable(cur=cur):
cur.execute("insert into points (x, y) values(1, 1)")
cur.execute("insert into points (x, y) values(1, 2)")
cur.execute("insert into points (x, y) values(2, 1)")
for row in cur.execute("select x, y from points"):
print(row)
for row in cur.execute("select sum(x * y) from points"):
print(row)
os.remove(file)
Файл "c: \ Users \ You_A \ Desktop \ 2019Coding \ context_generator_decorator.py", строка 32, в
os.remove (файл)
PermissionError: [WinError 32] Процесс не может получить доступ к файлу, поскольку он используется другим процессом: 'test.db'
Опять же, запуск os.remove("test.db")
в любом терминале успешно удаляет файл.