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批量加密文件的更多相关文章

  1. python批量进行文件修改操作

    python批量修改文件扩展名 在网上下载了一些文件,因为某种原因,扩展名多了一个后缀'.xxx',手动修改的话因为文件太多,改起来费时费力,于是决定写个小脚本进行修改. 1.要点: import r ...

  2. python 批量创建文件及文件夹(文件夹里再创文件)

    python 批量创建文件及文件夹(文件夹里再创文件)思路:文件建到哪>文件名字叫啥>创建文件夹>去新建的文件下>新建文件>给文件里边写东西>写个反馈给控制台> ...

  3. python批量拷贝文件

    普通批量拷贝文件 import os import shutil import logging from logging import handlers from colorama import Fo ...

  4. python批量json文件转xml文件脚本(附代码)

    场景:在使用了mask rcnn跑实验后标注了大量地json格式文件,现在打算使用yolo和faster rcnn 跑实验 所以需要将之前地json文件转为xml     但是找了很久,没发现有批量处 ...

  5. python批量处理文件夹中文件的问题

    用os模块读取文件夹中文件 原来的代码: import osfrom scipy.misc import imread filenames=os.listdir(r'./unprocess')for ...

  6. python批量修改文件名称

    参考文章:http://www.cnblogs.com/ma6174/archive/2012/05/04/2482378.html 最近遇到一个问题,在网上下载了一批视频课程,需要将每节课的名称标号 ...

  7. python批量删除文件

    敲代码測试时总会碰到要删除日志目录下的日志或者删除一些历史文件.每次都会生成,再測试的时候为了查找错误原因方便总是要在測试前删除这些文件.手动删除比較麻烦.所以写一个批量删除脚本 import os ...

  8. python批量删除文件夹

    制作的python程序跑一次就占200多内存在temp下面,关键是还不释放,最开始都没有发现这个问题,知道自己的c盘越来越小才发现问题所在.所以就有了去删除temp下生成的文件 代码如下: impor ...

  9. python 批量下载文件

    file.txt 的内容为: http://183.xxx.xxx.54:188/my/qqq.ico::qq.exe::0::http://183.xxx.xxx.54:186/my/ddnf.ic ...

随机推荐

  1. 不会提交 PR 的小伙伴看过来,超详细的视频教程!

    点击上方 蓝字关注我们 作者 | 严天奇 ✎ 编 者 按 最近有一些新加入社区的朋友反馈不太了解 Apache DolphinScheduler 提交 PR 的步骤和规则.这不,人帅心美的严天奇同学就 ...

  2. 重构、插件化、性能提升 20 倍,Apache DolphinScheduler 2.0 alpha 发布亮点太多!

    点击上方 蓝字关注我们 社区的小伙伴们,好消息!经过 100 多位社区贡献者近 10 个月的共同努力,我们很高兴地宣布 Apache DolphinScheduler 2.0 alpha 发布.这是 ...

  3. Apache Dolphinscheduler 1.3.x 系列配置文件指南

    前言 本文档为dolphinscheduler配置文件指南,针对版本为 dolphinscheduler-1.3.x 版本. 考虑公众号对markdown文件格式支持不那么友好的问题,建议大家在PC端 ...

  4. Two---python循环语句/迭代器生成器/yield与return/自定义函数与匿名函数/参数传递

    python基础02 条件控制 python条件语句是通过一条或多条语句的执行结果(Ture或者False)来执行的代码块 python中用elif代替了else if,所以if语句的关键字为:if- ...

  5. BZOJ2286/Luogu2495 [Sdoi2011]消耗战 (虚树)

    // never forget open "Head.cpp", boy, never ! #include <iostream> #include <cstdi ...

  6. 使用Linux、Nginx和Github Actions托管部署ASP.NET Core 6.0应用

    使用Linux.Nginx和Github Actions托管部署ASP.NET Core 6.0应用 前言 本文主要参考微软这篇文档而来 Host ASP.NET Core on Linux with ...

  7. 人非圣贤孰能无过,Go lang1.18入门精炼教程,由白丁入鸿儒,Go lang错误处理机制EP11

    人非圣贤,孰能无过,有则改之,无则加勉.在编程语言层面,错误处理方式大体上有两大流派,分别是以Python为代表的异常捕获机制(try....catch):以及以Go lang为代表的错误返回机制(r ...

  8. 蕞短鹭(artskjid) (⭐通信题/模拟⭐)

    文章目录 题面(过于冗长,主要是对通信题的一些解释) 题解 1.通信题什么意思 2.此题题解 CODE 实现 题面(过于冗长,主要是对通信题的一些解释) 题解 1.通信题什么意思 并不是两个程序同时跑 ...

  9. Codeforces Round #606(B-D)

    Dashboard - Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4) - Codeforces ...

  10. docker 匿名和具名挂载

    匿名挂载,只指定容器内了,没指定容器外 -v 容器内路径 docker run -d -P --name nginx01 -v /etc/nginx nginx #-P 随机映射端口 ; -v 不指定 ...