python代码加密

1、转为.pyc文件

# 将py文件转为pyc文件,并且显示到当前目录
python3 -m compileall -b .
# 删除目录下所有的py文件
find . -name "*.py" |xargs rm -rf
# 删除目录下所有的__pycache__文件
find . -name "__pycache__" |xargs rm -rf

参考:https://www.cnblogs.com/TTyb/p/6741848.html

使用python代码如下:不过可能报错  magic 。。。,不行就用上述命令

import compileall
compileall.compile_dir('.')

import time
time.sleep(4)

operate_dir = r'BERT-NER-master'

import os
for root,dirs,files in os.walk(operate_dir):
    for file in files:
        print('*' * 10)
        #获取文件所属目录
        print(root)
        #获取文件路径
        print(os.path.join(root,file))

        cur_file = os.path.join(root, file)
        if cur_file.endswith('.pyc'):
            print('cur_file:', cur_file)
            file_spt = file.split(r'.')

            print([file_spt[0], ] + file_spt[2:])

            new_file = os.path.join(root, '.'.join([file_spt[0], ] + file_spt[2:]))            
            print('new_file:', new_file)

            with open(new_file, 'wb') as fw:
                with open(cur_file, 'rb') as fr:
                    fw.write(fr.read())

            os.remove(cur_file)
        if cur_file.endswith('.py'):
            os.remove(cur_file)


import os
for root,dirs,files in os.walk(operate_dir):
    print('*' * 100)
    print(dirs)

    for one_dir in dirs:
        if one_dir == '__pycache__':
            cur_dir = os.path.join(root, one_dir)
            print('cur_dir:', cur_dir)

            os.system('cp %s/* %s/.. '%(cur_dir, cur_dir))

2、转为.so文件

https://zhuanlan.zhihu.com/p/94448855

发布了84 篇原创文章 · 获赞 149 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/feifeiyechuan/article/details/104963738