python批量加密文件
1.文件名的加密与解密
#coding:utf-8
from docx import Document
import os,sys
from docx.oxml.ns import qn
def file_name(user_dir,style):
file_list = list()
for root, dirs, files in os.walk(user_dir):
for file in files:
if (os.path.splitext(file)[1] == style):
file_list.append(os.path.join(root, file))
return file_list
def lock(path_file):
#doc = Document(path_file)
for i in range(len(path_file)):#找到不含地址的文件名
if(path_file[-i-1]=='\\'):
name = path_file[-i:]
path_output = path_file[:-i]
break
#print(path_output+name)
if name!= sys.argv[0]: #代码本身文件路径,防止脚本文件放在path路径下时,被一起重命名
os.rename(os.path.join(path_output,name),os.path.join(path_output,str(list(map(ord,name))))) #重命名文件
def unlock(path_file):
for i in range(len(path_file)):#找到不含地址的文件名
if(path_file[-i-1]=='\\'):
name = path_file[-i:]
path_output = path_file[:-i]
break
if name!= sys.argv[0]: #代码本身文件路径,防止脚本文件放在path路径下时,被一起重命名
os.rename(os.path.join(path_output,name),os.path.join(path_output,''.join(list(map(chr,eval(name)))))) #重命名文件
def lock_files(path):
print('输入需要加密的文件后缀:')
style = input()
path_files = file_name(path,'.'+style)
#print(path_files)
for path_file in path_files:
lock(path_file)
def unlock_files(path):
path_files = file_name(path,'')
#print(path_files)
for path_file in path_files:
unlock(path_file)
if __name__ == '__main__':
global path
path = '.\\'
print('菜单:\n1.加密\n2.解密')
n = eval(input())
if(n==1):
lock_files(path)
else:
unlock_files(path)
2.docx文件名和文本内容的加密与解密
#coding:utf-8
from docx import Document
import os,sys
from docx.oxml.ns import qn
list_sort = ['\\Ethics\\','\\Famous\\','\\Daily\\']
def file_name(user_dir):
file_list = list()
for root, dirs, files in os.walk(user_dir):
for file in files:
if (os.path.splitext(file)[1] == '.docx' or os.path.splitext(file)[1] == ''):
file_list.append(os.path.join(root, file))
return file_list
def lock(path_file):
doc = Document(path_file)
doc_ascii = Document()
doc_ascii.styles['Normal'].font.name = u'宋体'
doc_ascii.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
for i in list_sort:
if i in path_file:
sort = i
for value in doc.paragraphs: #遍历文档的每一段
text = value.text
ascii_str = list(map(ord, text))# 字符串 --> ascii
if(ascii_str==[]):
continue
doc_ascii.add_paragraph(str(ascii_str))
for i in range(len(path_file)):#找到不含地址的文件名
if(path_file[-i-1]=='\\'):
name = path_file[-i:]
break
#print(name)
path_output = path + '加密' + sort
if not os.path.exists(path_output):
os.makedirs(path_output)#自动创建文件夹
doc_ascii.save(path_output + str(list(map(ord,name))) )
def unlock(path_file):
doc = Document(path_file)
doc_new = Document()
doc_new.styles['Normal'].font.name = u'宋体'
doc_new.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
for i in list_sort:
if i in path_file:
sort = i
for code in doc.paragraphs:
code = eval(code.text)
prot_str = ''.join(map(chr,code))# ascii --> 字符串
doc_new.add_paragraph(prot_str)
for i in range(len(path_file)):#找到不含地址的文件名
if(path_file[-i-1]=='\\'):
name = path_file[-i:]
break
path_output = path + '解密' + sort
if not os.path.exists(path_output):
os.makedirs(path_output)
doc_new.save(path_output + ''.join(list(map(chr,eval(name)))))
def lock_files(path):
path_files = file_name(path)
#print(path_files)
for path_file in path_files:
lock(path_file)
def unlock_files(path):
path_files = file_name(path)
#print(path_files)
for path_file in path_files:
unlock(path_file)
if __name__ == '__main__':
global path
path = '.\\'
lock_files(path)
#unlock_files(path + '加密\\')
3.在文件名中插入随机数
import os,sys
from random import randint #导入模块
def add_prefix_files(): #定义函数名称
old_names = os.listdir(path) #取路径下的文件名,生成列表
for old_name in old_names: #遍历列表下的文件名
if old_name!= sys.argv[0]: #代码本身文件路径,防止脚本文件放在path路径下时,被一起重命名
if old_name.endswith('.mp4'): #当文件名以.txt后缀结尾时
temp = list(old_name)
for i in range(1,len(temp)):
if(temp[i] < 'z' and temp[i]<'a' or temp[i] < 'Z' and temp[i]<'A'):
continue
else:
num = randint(0,9)
temp[i] = str(num) + temp[i]
new_name = ''.join(temp)
os.rename(os.path.join(path,old_name),os.path.join(path,new_name)) #重命名文件
#print (old_name,"has been renamed successfully! New name is: ",mark+old_name) #输出提示
if __name__ == '__main__':
path = r'H:\\视频\\娱乐\\娱乐\\PPT\\' #运行程序前,记得修改主文件夹路径!
add_prefix_files() #调用定义的函数,注意名称与定义的函数名一致
4.六种文本加密编码的介绍
from urllib.parse import quote, unquote
import base64,hashlib
string = input()
# 1.utf-8
utf8_code = quote(string,encoding='utf-8') # 默认编码格式是utf-8
prot_str = unquote(utf8_code,encoding='utf-8')
print('utf-8:',utf8_code)
print('解码:',prot_str)
# 2.gbk
gbk_code = quote(string,encoding='gbk')# 设置编码格式
prot_str = unquote(gbk_code,encoding='gbk')# 解码
print('gbk:',gbk_code)
print('解码:',prot_str)
# 3.base64编码
b64_code = base64.b64encode(string.encode())# 编码:字符串 -> 二进制 -> base64编码
prot_str = base64.b64decode(b64_code).decode()# 解码:base64编码 -> 二进制 -> 字符串
print('base64编码:',b64_code)
print('解码:',prot_str)
# 4.ASCII码
ascii_str = list(map(ord, string))# 字符串 --> ascii
prot_str = ''.join(map(chr, ascii_str))# ascii --> 字符串
print('ASCII码:',ascii_str)
print('解码:',prot_str)
# 5.md5加密(md5加密后不可解密)
md5 = hashlib.md5()# 生成一个MD5对象
md5.update(string.encode('utf-8'))# 使用md5对象里的update方法md5转换
code = md5.hexdigest()# 得到加密后的字符串
print('md5密文:',code)
#6.unicode编码
unicode_str = string.encode("unicode_escape")# 编码
utf8_str = string.encode("utf-8")
gbk_str = string.encode("gbk")
print('unicode:',unicode_str)
print('utf8:',utf8_str)
print('gbk:',gbk_str)
# 解码
print('unicode解码:',unicode_str.decode("unicode_escape"))
print('utf-8解码:',utf8_str.decode())
print('gbk解码:',gbk_str.decode("gbk"))
python批量加密文件的更多相关文章
- python批量进行文件修改操作
python批量修改文件扩展名 在网上下载了一些文件,因为某种原因,扩展名多了一个后缀'.xxx',手动修改的话因为文件太多,改起来费时费力,于是决定写个小脚本进行修改. 1.要点: import r ...
- python 批量创建文件及文件夹(文件夹里再创文件)
python 批量创建文件及文件夹(文件夹里再创文件)思路:文件建到哪>文件名字叫啥>创建文件夹>去新建的文件下>新建文件>给文件里边写东西>写个反馈给控制台> ...
- python批量拷贝文件
普通批量拷贝文件 import os import shutil import logging from logging import handlers from colorama import Fo ...
- python批量json文件转xml文件脚本(附代码)
场景:在使用了mask rcnn跑实验后标注了大量地json格式文件,现在打算使用yolo和faster rcnn 跑实验 所以需要将之前地json文件转为xml 但是找了很久,没发现有批量处 ...
- python批量处理文件夹中文件的问题
用os模块读取文件夹中文件 原来的代码: import osfrom scipy.misc import imread filenames=os.listdir(r'./unprocess')for ...
- python批量修改文件名称
参考文章:http://www.cnblogs.com/ma6174/archive/2012/05/04/2482378.html 最近遇到一个问题,在网上下载了一批视频课程,需要将每节课的名称标号 ...
- python批量删除文件
敲代码測试时总会碰到要删除日志目录下的日志或者删除一些历史文件.每次都会生成,再測试的时候为了查找错误原因方便总是要在測试前删除这些文件.手动删除比較麻烦.所以写一个批量删除脚本 import os ...
- python批量删除文件夹
制作的python程序跑一次就占200多内存在temp下面,关键是还不释放,最开始都没有发现这个问题,知道自己的c盘越来越小才发现问题所在.所以就有了去删除temp下生成的文件 代码如下: impor ...
- python 批量下载文件
file.txt 的内容为: http://183.xxx.xxx.54:188/my/qqq.ico::qq.exe::0::http://183.xxx.xxx.54:186/my/ddnf.ic ...
随机推荐
- pat链表专题训练+搜索专题
本期题目包括: 1074:https://pintia.cn/problem-sets/994805342720868352/problems/994805394512134144 1052:http ...
- 安装 NetworkManager(Debian最小化安装)
Debian最小化安装是没有NetworkManager 1 安装: 2 [root@debian]apt install -y netwok-manager 1 启动(查看网卡): 2 [root@ ...
- JavaSciprt 笔记目录
JavaScript 基础知识(一):对象以及原型 JavaScript 基础知识(二):闭包 JavaScript 异步编程(一):认识异步编程 JavaScript 异步编程(二):Promise
- identity4 系列————持久化配置篇[五]
前言 上面已经介绍了3个例子了,并且介绍了如何去使用identity. 但是在前面的例子中,我们使用的都是在内存中操作,那么正式上线可能需要持久到数据库中. 这里值得说明的是,并不一定一定要持久化到数 ...
- 【unity游戏入门】1 环境安装
作者 罗芭Remoo 2021年9月24日 这一个系列随笔记录如何使用unity引擎从零开始开发游戏,软件版本均使用最新版本 有关unity的背景介绍可以通过unity官网https://unity. ...
- Neural ODE相关论文摘要翻译
*****仅供个人学习记录***** Neural Ordinary Differential Equations[2019] 论文地址:[1806.07366] Neural Ordinary Di ...
- 第七十五篇:Vue兄弟组件传值
好家伙, 兄弟组件的传值用到Eventbus组件, 1.EventBus的使用步骤 ① 创建 eventBus.js 模块,并向外共享一个Vue的实例对象 ②在数据发送方, 调用bus.$emit(' ...
- C语言:多功能计算器 (矩阵相乘)
好家伙,实现矩阵相乘功能 代码如下: void fifth()//矩阵的相乘// { int a[100][100],b[100][100]; int d,e,f,h,j,k,t; double su ...
- 正点原子keilkill脚本
del *.bak /s del *.ddk /s del *.edk /s del *.lst /s del *.lnp /s del *.mpf /s del *.mpj /s del *.obj ...
- Windows DNS服务器策略
Windows 2016开始微软在Windows服务器中引入了针对DNS服务器的策略.可以方便灵活的控制DNS服务器响应客户端的请求.这里举个例子,阻止某个网段的DNS查询.思路是这样的,定义一个网段 ...