How to fix PermissionError: [WinError 32] with os.remove in python?
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(文件)
之前
这将允许 OS 访问该文件,就像之前 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(文件)
之前这将允许 OS 访问该文件,就像之前 python 持有该文件一样。