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. Spring的简单使用(3)

    一:SM框架的整合: 所需要的依赖: <dependency> <groupId>junit</groupId> <artifactId>junit&l ...

  2. 海豚调度直播来了 - 即将发版的1.3.0新特性及Roadmap路线

    在过去的3个多月,Apache DolphinScheduler(incuating)和DolphinScheduler社区发生了很多变化,今晚19:30在线直播将为大家介绍最新1.3.0的新特性及R ...

  3. 完整实现-通过DelayQueue实现延时任务

    实现延时任务有很多的方法,网上关于延时任务的实现的文章已经不少了.比如:实现延时任务的10种方法等等.但是这些文章基本上都是将方法大概的列举一下,给出部分示例代码,对于有经验的老程序员可能一看就知道该 ...

  4. JavaScript 基础知识(一):对象以及原型

    前言 JavaScript 常被描述为一种基于原型的语言--每个对象拥有一个原型对象,对象以其原型为模板.从原型继承方法和属性.原型对象也可能拥有原型,并从中继承方法和属性,一层一层.以此类推.这种关 ...

  5. ceph 010 clustermap ceph调优

    cluster map [ceph: root@clienta /]# ceph mon dump epoch 4 fsid 2ae6d05a-229a-11ec-925e-52540000fa0c ...

  6. mysql验证隔离级别

    mysql更改隔离级别 参考https://blog.csdn.net/qq_21299835/article/details/121189565 关闭mysql事务自动提交:set autocomm ...

  7. Python工具箱系列(五)

    上一期介绍了Anaconda的安装,本期介绍Miniconda的安装,它们共同的部分是Conda,确实如此.Conda是一个开源的包管理系统,本身的志向非常宏大,要为Python. R. Ruby. ...

  8. Github-CLI

    Github-CLI Github 的官方命令行工具 Github CLI.Mac 系统可以通过 homebrew 安装或者直接下载免安装包来使用. 命令 Github CLI 的所有命令均以gh开头 ...

  9. Kubernetes 监控:Prometheus Operator + Thanos ---实践篇

    具体参考网址:https://www.cnblogs.com/sanduzxcvbnm/p/16291296.html 本章用到的yaml文件地址:https://files.cnblogs.com/ ...

  10. 给 SSH 启用二次身份验证

    转载自:https://mp.weixin.qq.com/s/ssuhFbfaHxxzGmLg6Y2MjA 目前来说,二次验证(这里就不做过多解释了)是比较常用的安全手段,通过设置二次验证(谷歌或其他 ...