2
我嘗試使用下面的函數臨時圖像:PermissionError:[WinError 32]當試圖刪除
def image_from_url(url):
"""
Read an image from a URL. Returns a numpy array with the pixel data.
We write the image to a temporary file then read it back. Kinda gross.
"""
try:
f = urllib.request.urlopen(url)
_, fname = tempfile.mkstemp()
with open(fname, 'wb') as ff:
ff.write(f.read())
img = imread(fname)
os.remove(fname)
return img
except urllib.error.URLError as e:
print('URL Error: ', e.reason, url)
except urllib.error.HTTPError as e:
print('HTTP Error: ', e.code, url)
但我一直收到以下錯誤:
---> 67 os.remove(fname)
68 return img
69 except urllib.error.URLError as e:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\Nir\\AppData\\Local\\Temp\\tmp8p_pmso5'
無其他進程正在我的機器上運行(據我所知)。如果我遺漏了os.remove(fname)
函數,那麼代碼運行良好,但我不希望我的臨時文件夾填滿垃圾。
任何想法是什麼讓圖像被刪除?
讀取圖像數據到一個數組的東西...你先嚐試關閉它? –
你試過禁用AV嗎? – kichik
嘗試在提升的命令提示符下運行腳本。 –