Information Security 6 min read

Simple Python Antivirus Script Tutorial

This article narrates a real‑world incident where a frozen computer leads the author to develop a simple Python antivirus that scans directories, matches files against a virus list, and deletes infected files using the os module, illustrating basic information‑security scripting techniques.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Simple Python Antivirus Script Tutorial

Follow + star, learn new Python skills daily.

The story begins with a heavy‑duty operation that hasn't finished yet, so details are withheld. While holding a bowl of instant noodles, the author is called to a user whose computer is frozen.

He asks if he can finish his meal first, noting he hasn't eaten for three hours and is too hungry to walk.

The colleague says it's the director's computer and leaves it to him.

He reflects that although he doesn't fear authority, his schooling taught him to help others in trouble.

At the site he sees only an empty desktop with no mouse, indicating the system is extremely laggy.

He asks the user if any unusual actions were taken.

The user replies nothing; the computer shows the same state on boot.

He realizes the cause: after power‑cycling, three security programs (360, Computer Manager, Kingsoft Antivirus) occupy the taskbar, filling the desktop with a “full‑family bucket” of unwanted software, prompting a system reinstall.

On the way back he complains about the rogue software.

A Lenovo colleague retorts, “If you can, write one yourself.”

He decides to write a simple antivirus as a hobby.

Antivirus essentially deletes virus files, which can be done with Python’s os module.

<code>import os
os.remove(r'E://python/病毒.txt')</code>

Since there are many viruses, a virus database is needed; matching files are deleted.

<code>import os
病毒库 = ['病毒.txt']
path = r'E://python/病毒.txt'
if path in 病毒库:
    os.remove(path)
    print('已经删除病毒文件', file)</code>

To scan all files, a recursive function is required.

<code>def getAllFile(path):
    # 获取目录中的所有文件并遍历
    files = os.listdir(path)
    for file in files:
        # 拼接路径,如果是目录则递归获取所有文件
        new_path = path + r'/' + file
        if os.path.isdir(new_path):
            getAllFile(new_path)
        else:
            # 如果是文件则查询病毒库,匹配成功则删除
            if file in 病毒库:
                os.remove(new_path)
                print('已经删除病毒文件', file)</code>

The script asks the user for a directory to scan.

<code>path = input('请输入需要查杀的目录:')
getAllFile(path)</code>

The complete script is presented below.

<code>import os

病毒库 = ['病毒.txt']

def getAllFile(path):
    # 获取目录中的所有文件并遍历
    files = os.listdir(path)
    for file in files:
        # 拼接路径,如果是目录则递归获取所有文件
        new_path = path + r'/' + file
        if os.path.isdir(new_path):
            getAllFile(new_path)
        else:
            # 如果是文件则查询病毒库,匹配成功则删除
            if file in 病毒库:
                os.remove(new_path)
                print('已经删除病毒文件', file)

path = input('请输入需要查杀的目录:')
getAllFile(path)</code>

For testing, a dummy virus file is created.

Running the program and entering the target directory removes the infected file.

The virus is successfully deleted.

He humorously remarks on the triviality of the virus.

While stretching, he discovers his noodle has solidified.

*Disclaimer: This article is compiled from the internet; copyright belongs to the original author. If any source information is incorrect or infringes rights, please contact us for removal or authorization.

information securityTutorialantivirusos modulefile scanning
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.