在python中移动文件时发生permissionerror

k5ifujac  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(201)

我有一个非常简单的python病毒扫描程序,但是当扫描完成并且结果写入控制台时,我想移动文件并对其进行加密。但是它说:permissionerror:[winerror 32]进程无法访问该文件,因为它正被另一个进程使用:“c:\users\sepehr\desktop\Virus\malware.exe”。
md5_list.txt=受感染哈希的列表
这是我的代码:

from cryptography.fernet import Fernet
import hashlib
import shutil
import random
import string

def singlefile_scan():
    Result = 'Clean!'
    scan_this = 'anything'
    ll = 'ENDOFFILE'
    scan_this = input('Path of your file(Do not forget to put your filename after the path with slash): ')
    print("OK,Let's scan it...")
    md5_hash = hashlib.md5()
    a_file = open(scan_this , "rb")
    content = a_file.read()
    md5_hash.update(content)
    digest = md5_hash.hexdigest()
    while Result == 'Clean!':
    f = open('C:\Program Files\av\Data\MD5_list.txt')
    for line in f:
        if digest == line.strip():
            Result = 'Infected!'
        else:
            if line == ll: #ll= last line
            break

    print(scan_this + ":",Result)
    f.close()
    if Result == 'Infected!':
        yn = input("Shall I quarantine the malware ? (y/n) ")
        if yn == 'y':
            characters = string.ascii_letters + string.digits
            password = ''.join(random.choice(characters) for i in range(8))
            dot = '.'
            shutil.move(scan_this, 'C:/Program Files/av/Quarantine/' +  password + dot + 'mal')
            os.chdir("C:\Program Files\av\Quarantine")
            with open('C:/Program Files/av/Data/filekey.key', 'rb') as filekey:
                key = filekey.read()
                fernet = Fernet(key)
            with open(password + dot + 'mal', 'rb') as file:
                original = file.read()
            encrypted = fernet.encrypt(original)
            with open(password + dot + 'mal', 'wb') as encrypted_file:
                encrypted_file.write(encrypted)
                infected_file_name = password + dot + 'mal'
            print("File quarantined: {}".format(scan_this))
singlefile_scan()

发生了什么,如何解决?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题