1 Star 0 Fork 0

Python程序设计/20181221_zengyutao

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
文件操作.py 3.73 KB
一键复制 编辑 原始数据 按行查看 历史
曾宇涛 提交于 2020-04-22 23:35 . 文件操作
import os
import binascii
from docx import Document
from pyDes import des, CBC, PAD_PKCS5
def mkdir(path):
path = path.strip()
path = path.rstrip("\\")
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
print(path + ' 创建成功')
return True
else:
print(path + ' 目录已存在')
return False
def mkfile(path):
path = path.strip()
path = path.rstrip("\\")
isExists = os.path.exists(path)
if not isExists:
file= open(path,'w')
print(path + ' 创建成功')
file.close()
print("文件已关闭")
return True
else:
print(path + ' 文件已存在')
return False
def write(path):
path = path.strip()
path = path.rstrip("\\")
isExists = os.path.exists(path)
if not isExists:
mkfile(path)
write(path)
else:
file = open(path, 'a')
what = input("请输入您想写入的文字:\n")
file.write(what+'\n')
print('写入成功')
file.close()
print("文件已关闭")
def read(path):
path = path.strip()
path = path.rstrip("\\")
isExists = os.path.exists(path)
if not isExists:
mkfile(path)
read(path)
else:
file = open(path, 'r')
print(file.read())
file.close()
print("文件已关闭")
def word(path):
Docx = Document()
Docx.add_heading("姓名 学号 性别 班级 成绩 健康")
Docx.add_heading("张三 20190001 男 201900 99 健康")
Docx.add_heading("李四 20190002 男 201900 98 健康")
Docx.save(path)
document = Document(path)
for paragraph in document.paragraphs:
print(paragraph.text)
def des_encrypt():
KEY = 'areyouok'
def des_descrypt(s):
secret_key = KEY
iv = secret_key
k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
de = k.decrypt(binascii.a2b_hex(s), padmode=PAD_PKCS5)
de = str(de)
Docx = Document()
Docx.add_paragraph(de)
Docx.save("C:\\Users\\misaka\\Desktop\\北京电子科技学院\\python\\1812\\解密文件.doc")
Docx = Document()
Docx.add_heading("This is my word!")
Docx.save("C:\\Users\\misaka\\Desktop\\北京电子科技学院\\python\\1812\\明文.doc")
path = "C:\\Users\\misaka\\Desktop\\北京电子科技学院\\python\\1812\\明文.doc"
document = Document(path)
for paragraph in document.paragraphs:
s = paragraph.text
secret_key = KEY
iv = secret_key
k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
en = k.encrypt(s, padmode=PAD_PKCS5)
en = binascii.b2a_hex(en)
des_descrypt(en)
en = str(en)
Docx = Document()
Docx.add_paragraph(en)
Docx.save("C:\\Users\\misaka\\Desktop\\北京电子科技学院\\python\\1812\\密文.doc")
def exel():
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
path = "C:\\Users\\misaka\\Desktop\\北京电子科技学院\\python\\1812\\20181221.doc"
document = Document(path)
i=-1
for paragraph in document.paragraphs:
i+=1
k=-1
list=paragraph.text.split();
for j in list:
k+=1
ws.write(i, k, j)
wb.save('C:\\Users\\misaka\\Desktop\\北京电子科技学院\\python\\1812\\20181221.xls')
if __name__ == "__main__":
mkpath = "C:\\Users\\misaka\\Desktop\\北京电子科技学院\\python\\1812"
mkdir(mkpath)
filename = "C:\\Users\\misaka\\Desktop\\北京电子科技学院\\python\\1812\\20181221.txt"
mkfile(filename)
# write(filename)
read(filename)
word("C:\\Users\\misaka\\Desktop\\北京电子科技学院\\python\\1812\\20181221.doc")
des_encrypt()
exel()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python_programming/zengyutao20181221.git
[email protected]:python_programming/zengyutao20181221.git
python_programming
zengyutao20181221
20181221_zengyutao
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385