1、编写代码,查看zip和rar压缩文件中的文件列表 zipfile.ZipFile()打开压缩包的方法
1 ''' 2 编写代码,查看zip和rar压缩文件中的文件列表 3 zipfile.ZipFile()打开压缩包的方法 4 ''' 5 import zipfile 6 7 with zipfile.ZipFile(r'D:\test\test.zip') as fp: 8 for f in fp.namelist(): 9 print(f)
2、Python扩展库tarfile提供了对tar文件的访问
''' Python扩展库tarfile提供了对tar文件的访问 ''' import tarfile import rarfile as rarfile with tarfile.TarFile(r'D:\test\test.tar') as fp: for i in fp: print(i)
3、小学数学算术题生成器
import random import os import tkinter import tkinter.ttk from docx.shared import Pt from docx import Document columnsNumber = 4 def main(rowsNumbers, grade): if grade < 3: operators = '+-' Max = 20 elif grade <= 4: operators = '+-×÷' Max = 100 elif grade == 5: operators = '+-×÷(' Max = 100 document = Document() table = document.add_table(rows=rowsNumbers, cols=columnsNumber) table.style.font.name = '微软雅黑' table.style.font.size = Pt(10) for row in range(rowsNumbers): for col in range(columnsNumber): first = random.randint(1, Max) second = random.randint(1, Max) operator = random.choice(operators) if operator != '(': # 不是五年级 if operator == '-' or '÷': if first < second: first, second = second, first r = str(first).ljust(2, ' ') + ' ' + operator + str(second).ljust(2, ' ') + '=' else: # 是五年级 third = random.randint(1, 100) while True: o1 = random.choice(operators) o2 = random.choice(operators) if o1 != '(' and o2 != '(': break # 考虑括号的口算题 r2 = random.randint(1, 100) if r2 > 50: if o2 == '-': if second < third: second, third = third, second r = str(first).ljust(2, ' ') + o1 + '(' + str(second).ljust(2, ' ') + o2 + str(third).ljust(2, ' ') + ')=' else: if o1 == '-': if first < second: first, second = second, first r = '(' + str(first).ljust(2, ' ') + o1 + str(second).ljust(2, ' ') + ')' + o2 + str(third).ljust(2, ' ') + '=' cell = table.cell(row, col) cell.text = r document.save('小学生口算题.docx') os.startfile("小学生口算题.docx") if __name__ == '__main__': app = tkinter.Tk() app.title('小学生口算题生成器') app['width'] = 300 app['height'] = 150 lableNumber = tkinter.Label(app, text='Number:', justify=tkinter.RIGHT, width=50) lableNumber.place(x=10, y=40, width=50, height=20) comboNumber = tkinter.ttk.Combobox(app, values=(100, 200, 300, 400, 500), width=50) comboNumber.place(x=70, y=40, width=50, height=20) labelGrade = tkinter.Label(app, text='Grade:', justify=tkinter.RIGHT, width=50) labelGrade.place(x=130, y=40, width=50, height=20) comboGrade = tkinter.ttk.Combobox(app, values=(1, 2, 3, 4, 5), width=50) comboGrade.place(x=200, y=40, width=50, height=20) def generate(): number = int(comboNumber.get()) grade = int(comboGrade.get()) rowsNumbers = int(number / 4) main(rowsNumbers, grade) buttonGenerate = tkinter.Button(app, text='Go', width=40, command=generate) buttonGenerate.place(x=130, y=90, width=40, height=30) app.mainloop()
4、编写程序,统计文件夹中所有pptx格式的PowerPoint文件中幻灯片数量
''' 编写程序,统计文件夹中所有pptx格式的PowerPoint文件中幻灯片数量 ''' import os import pptx total = 0 def pptCount(path): global total #导出path路径下的所有文件 for subPath in os.listdir(path): #为每一个文件制作绝对路径 subPath = os.path.join(path,subPath) #如果是一个文件夹,就进入文件夹,通过递归调用 if os.path.isdir(subPath): pptCount(subPath) #如果是一个文件,并且以.pptx结尾,先输入这个文件绝对路径,随后统计幻灯片的数量 elif subPath.endswith('.pptx'): print(subPath) #统计ppt里面幻灯片数量 presentation = pptx.Presentation(subPath) total += len(presentation.slides) pptCount(r'D:\test') print(total)
5、编写程序,检测U盘插入并自动复制全部文件
''' 编写程序,检测U盘插入并自动复制全部文件 ??? ''' from time import sleep from shutil import copytree from psutill import disk_partitions while True: sleep(3) #检查所有驱动器 for item in disk_partitions(): #发现可移动驱动 if 'removable' in item.opts: driver = item.device #输出可移动驱动器符号 print('Found USB disk:',driver) break else: continue break #复制根目录 copytree(driver,r'D:\usbdriver') print('all files copied.')
标签:文件,tkinter,处理,app,50,width,second,import From: https://www.cnblogs.com/buchijiuminvhai/p/17013886.html