前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python实用文件操作

Python实用文件操作

作者头像
算法与编程之美
发布2023-08-22 14:32:03
1350
发布2023-08-22 14:32:03
举报
文章被收录于专栏:算法与编程之美

1 问题

用python写一些方便实用的脚本,方便批量操作一些文件。

2 方法

先学会如何导入,用代码建立不同软件,文件与python的联系。学会自带的功能或导入别人制作的优秀库。可以自己学习定义,编写函数操作。

代码清单 1

加水印from PIL import Imagefrom PIL import ImageFontfrom PIL import ImageDrawdef watermark_Image(img_path,output_path, text, pos): img = Image.open(img_path) drawing = ImageDraw.Draw(img) black = (10, 5, 12) drawing.text(pos, text, fill=black) img.show() img.save(output_path)img = '2.png'watermark_Image(img, 'watermarked_2.jpg','Python', pos=(10, 10))检测文本相似性from difflib import SequenceMatcherdef file_similarity_checker(f1, f2): with open(f1, errors="ignore") as file1, open(f2, errors="ignore") as file2: f1_data = file1.read() f2_data = file2.read() checking = SequenceMatcher(None, f1_data, f2_data).ratio() print(f"These files are {checking*100} % similar")file_1 = "路径1"file_2 = "路径2"file_similarity_checker(file_1, file_2)加密from cryptography.fernet import Fernetdef encrypt(filename, key): fernet = Fernet(key) with open(filename, 'rb') as file: original = file.read() encrypted = fernet.encrypt(original) with open(filename, 'wb') as enc_file: enc_file.write(encrypted)key = Fernet.generate_key()filename = "file.txt"encrypt(filename, key)def decrypt(filename, key): fernet = Fernet(key) with open(filename, 'rb') as enc_file: encrypted = enc_file.read() decrypted = fernet.decrypt(encrypted) with open(filename, 'wb') as dec_file: dec_file.write(decrypted)import pyAesCryptdef Encryption(input_file_path, output_file_path, key): pyAesCrypt.encryptFile(input_file_path, output_file_path, key) print("File has been decrypted")def Decryption(input_file_path, output_file_path, key): pyAesCrypt.decryptFile(input_file_path, output_file_path, key) print("File has been decrypted")decrypt(filename, key)

3 结语

需要多学习了解一些有用的扩展库和模块。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-07-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 算法与编程之美 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档