[英]How to fix PermissionError: [WinError 32] with os.remove in python?
我的代碼中出現PermissionError:[WinError 32]問題。 必須先將文件解密,然后再刪除(加密)。 但是該文件僅被解密,並且文件夾中保留了兩個文件:已加密和已解密。
可能是什么問題呢?
我已經嘗試過在解密和刪除之間延遲。
def decrypt(file):
file_in = open(file, "rb")
file_out = open(str(file[:-4]), "wb")
rivate_key = RSA.import_key(open("private.pem").read())
enc_session_key, nonce, tag, ciphertext = \
[ file_in.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1) ]
cipher_rsa = PKCS1_OAEP.new(private_key)
session_key = cipher_rsa.decrypt(enc_session_key)
cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
data = cipher_aes.decrypt_and_verify(ciphertext, tag)
file_out.write(data)
print(file + " DECRYPT!")
time.sleep(5)
os.remove(file)
出現以下錯誤:
>Traceback (most recent call last):
File "C:/Users/user/Desktop/codes on python/decrypt.py", line 31, in <module>
walk("C:\\Users\\user\\Desktop\\codes on python\\papka")
File "C:/Users/user/Desktop/codes on python/decrypt.py", line 28, in walk
if os.path.isfile(path): decrypt(path)
File "C:/Users/user/Desktop/codes on python/decrypt.py", line 23, in decrypt
os.remove(file)
PermissionError: [WinError 32] Процесс не может получить доступ к файлу, так как этот файл занят другим процессом: 'C:\\Users\\user\\Desktop\\codes on python\\papka\\data.data.bin'
必須先關閉文件,然后才能刪除它們
您必須先運行: file_in.close()才能運行os.remove(file)
與python保留文件之前一樣,這將允許操作系統訪問文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.