Python 随机生成文本文件并加密:创建目录、随机行数和字符数
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/nYja 著作权归作者所有。请勿转载和采集!