读取zip文件
import zipfile
listAipInfo = zipfile.ZipFile('xxx.zip', 'r')
print(listZipInfo.namelist()) # 以列表列出所有压缩文件
列出所有被压缩的文件,以及文件名、文件大小和压缩结果大小。
for info in listZipInfo.infolist():
print(info.filename, info.file_size, info.compress_size)
fileUnzip = zipfile.ZipFile('out.zip')
fileUnzip.extractall('out43')
fileUnzip.close()
剪切板应用
import pyperclip
pyperclip.copy("xxxxx")
string = pyperclip.paste()
print(string)
找不到文件的错误FileNotFoundError
fn = 'xxxx.txt'
try:
with open(fn) as file_obj:
data = file_obj.read()
except FileNotFoundError:
print("找不到文件")
else:
print(data)
使用tkinter处理谢尔宾斯基三角形
from tkinter import *
# 依据特定阶数绘制sierpinski三角形
def sierpinski(order, p1, p2, p3):
if order == 0: # 直接三个点相连绘制三角形
drawLine(p1, p2)
drawLine(p2, p3)
drawLine(p3, p1)
else:
# 取各个三角形的中点
p12 = midpoint(p1, p2)
p23 = midpoint(p2, p3)
p31 = midpoint(p3, p1)
# 递归调用处理绘制三角形
sierpinski(order-1, p1, p12, p31)
sierpinski(order-1, p12, p2, p23)
sierpinski(order-1, p31, p23, p3)
# 绘制直线
def drawLine(p1, p2):
canvas.create_line(p1[0], p1[1], p2[0], p2[1])
# 计算中间值
def midpoint(p1, p2):
p = [0, 0]
p[0] = (p1[0] + p2[0]) / 2
p[1] = (p1[1] + p2[1]) / 2
return p
def show():
canvas.delete("myline")
p1 = [200, 20]
p2 = [20, 380]
p3 = [380, 380]
sierpinski(order.get(), p1, p2, p3)
tk = Tk()
canvas = Canvas(tk, width=400, height=400)
canvas.pack()
frame = Frame(tk)
frame.pack(padx=5, pady=5)
# 在框架内建立标签label,输入阶数Entry,按钮button
Label(frame, text="输入阶数:").pack(side=LEFT)
order = IntVar()
order.set(0)
entry = Entry(frame, textvariable=order).pack(side=LEFT, padx=3)
Button(frame, text="显示Sierpinski三角形", command=show).pack(side=LEFT)
tk.mainloop()