用python编写程序将创建一个文本文件1txt到 d Python 如果目录不存在则创建该目录要求:1文件行数:随机范围[10-20;2每行字符数:随机范围[10-30;中所有字母进行加密到另一个文件加密规则原文对 应的密文为: 原文: abcdefghi jklmnopqrstuvwxyz 密文: nopqrstuvwxyzabcdefghi jklm
import os import random
密码表
encrypt_table = str.maketrans("abcdefghijklmnopqrstuvwxyz", "nopqrstuvwxyzabcdefghijk")
创建目录和文件
if not os.path.exists("d:\Python"): os.mkdir("d:\Python") with open("d:\Python\1.txt", "w") as f: # 随机行数 line_num = random.randint(10, 20) for i in range(line_num): # 随机每行字符数 char_num = random.randint(10, 30) line = "" for j in range(char_num): # 随机生成小写字母 line += chr(random.randint(97, 122)) f.write(line + "\n")
加密文件
with open("d:\Python\1.txt", "r") as f1, open("d:\Python\2.txt", "w") as f2: for line in f1: f2.write(line.translate(encrypt_table)
原文地址: https://www.cveoy.top/t/topic/eK4T 著作权归作者所有。请勿转载和采集!