Python趣味实用小工具
数码 2
本文将从多个方面对Python趣味实用小工具进行详细的阐述,介绍实用的小工具应用、技巧以及提高编程能力的方法。
1、工具介绍:随机密码生成器,可以自动生成包含大写字母、小写字母、数字、符号的密码。
2、代码示例:
import random
import string
def random_password(len):
password = []
while len > 0:
password.append(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits + string.punctuation))
len -= 1
return "".join(password)
print("生成密码长度为10:", random_password(10))
print("生成密码长度为15:", random_password(15))
3、技巧说明:代码中使用了random和string模块,其中random模块用来生成随机字符,string模块则包含所有的大写字母、小写字母、数字和标点符号。
1、工具介绍:将图片转换成由字符构成的字符画。
2、代码示例:
from PIL import Image
# 将RGB值转换为灰度值
def rgb2gray(rgb):
return int(0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2])
# 将灰度值转换为字符
def gray2char(gray):
charList = list("WBM@%-+*:. ")
charLen = len(charList)
unit = 255 // (charLen - 1)
return charList[int(gray/unit)]
if __name__ == '__main__':
img = Image.open("test.jpg")
img = img.resize((160, 90))
width, height = img.size
txt = ""
for y in range(height):
for x in range(width):
dot = img.getpixel((x, y))
txt += gray2char(rgb2gray(dot))
txt += "\n"
print(txt)
3、技巧说明:代码中使用了pillow库来处理图片,首先将图片调整为指定大小,然后对图片进行灰度处理,最后将灰度转换成字符。
1、工具介绍:Python有多个画图库,如matplotlib、seaborn等。
2、代码示例:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
#绘制线性图
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Linear graph')
plt.show()
#绘制散点图
plt.scatter(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter plot')
plt.show()
3、技巧说明:代码中使用了matplotlib.pyplot库来进行数据可视化,其中plot函数可以绘制线性图,scatter函数可以绘制散点图。
1、工具介绍:可以对多个文件进行重命名、删除、拷贝等批量操作。
2、代码示例:
import os
path = "./"
# 批量重命名
def rename():
i = 0
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
# 修改文件名
newname = "new_" + str(i) + ".txt"
os.rename(os.path.join(path, file), os.path.join(path, newname))
i += 1
# 批量删除
def delete():
for file in os.listdir(path):
if os.path.isfile(os.path.join(path,file)):
os.remove(os.path.join(path, file))
# 批量拷贝
def copy():
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
newfile = os.path.join(path, "copy_" + file)
# 拷贝文件
with open(os.path.join(path, file), "rb") as f1, open(newfile, "wb") as f2:
f2.write(f1.read())
print("操作完成!")
if __name__ == '__main__':
rename()
delete()
copy()
3、技巧说明:os模块用于文件系统操作,通过os.listdir函数可以列出指定文件夹下所有文件,然后可以进行重命名、删除、拷贝操作。
1、工具介绍:可以提取多个文件的基本信息,如文件名、路径、大小、创建时间等。
2、代码示例:
import os
path = "./"
# 批量提取文件信息
def file_info():
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
file_path = os.path.join(path, file)
# 文件名
filename = os.path.splitext(file)[0]
# 扩展名
extname = os.path.splitext(file)[1]
# 文件大小
size = os.path.getsize(file_path)
# 创建时间
create_time = os.path.getctime(file_path)
create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(create_time))
# 最后修改时间
modify_time = os.path.getmtime(file_path)
modify_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(modify_time))
# 输出文件信息
print("文件名:", filename)
print("扩展名:", extname)
print("文件大小:", size, "字节")
print("创建时间:", create_time)
print("最后修改时间:", modify_time)
print("\n")
if __name__ == '__main__':
file_info()
3、技巧说明:代码中使用了os模块和time模块,os模块可以获取文件的基本信息,time模块可以将时间戳转换为指定格式的日期时间。