python: FileHelper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | # encoding: utf-8 # 版权所有 2023 涂聚文有限公司 # 许可信息查看: # 描述: # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2023.1 python 311 # Datetime : 2023/7/9 19:12 # User : geovindu # Product : PyCharm # Project : pythonTkinterDemo # File : FileLib.py # explain : 学习 import os import sys import shutil from PIL import Image,ImageSequence import glob import pyzbar.pyzbar as p import pyqrcode import qrcode class FileHelper( object ): """ 简单的文件帮助 """ global cdir global cfile global allfiles def __init__( self ): self .__path = "" self .cdir = 1 self .cfile = 1 self .allfiles = list () @property def Path( self ): return self .__path @Path .setter def Path( self ,path): self .__path = path def dirAllFile( self ,pathname): """ 迭代遍历文件夹下的子文件夹和文件 iterate :param pathname: 文件夹的地址 :return:返回最后一次文件夹的文件 """ files = list () if os.path.exists(pathname): filelist = os.listdir(pathname) for f in filelist: f = os.path.join(pathname, f) if os.path.isdir(f): self .cdir + = 1 self .dirAllFile(f) else : dirname = os.path.dirname(f) baseName = os.path.basename(f) if os.path.isfile(f): files.append(f) self .cfile + = 1 self .allfiles.append(files) return files #最后一次遍历的 def traversalFiles( self ): """ 只是当前文件夹下的,子文件夹的没有列出 :return: """ dirs = [] files = [] #global cdir=1 #global cfile=1 for item in os.scandir( self .__path): if item.is_dir(): dirs.append(item.path) cdir + = 1 elif item.is_file(): files.append(item.path) cfile + = 1 #traversalFiles(self) print ( 'dirs:' ) print ( '\n' .join(dirs)) print () print ( 'files:' ) print ( '\n' .join(files)) print (f "文件夹个数:{cdir},文件个数:{cfile}:" ) def displayFileLsit( self ): """ :return: """ paths = os.walk(f '{self.__path}' ) cou = 0 for path, dir_lst, file_lst in paths: for file_name in file_lst: print (os.path.join(path, file_name)) cou + = 1 print ( "共有文件:" , cou) # 创建文件方法 def mkdirFile( self , writestring: str ): """ 没有文件夹创建,把内容写入TEXT文本文件中 :param folder: 文件夹名称 :param writestring: 要写的内容 :return: None """ i = 1 while True : # 判断是否文件存在,如果不存在则创建,存在则改名 f_p_intact = self .__path + "\\geovindu" + str (i) + ".txt" # 打印完整文件名称 print ( "已创建的文件是:" ,f_p_intact) if not os.path.exists(f_p_intact): # 文件的写操作 f = open (f_p_intact, "w+" ,encoding = 'utf8' ) f.write(writestring) # 关闭文件流 f.close() break else : i + = 1 def makeGif( self ): """ 生成GIF动画图片 :param framefolder: 文件夹 :return: """ images = glob.glob(f "{self.__path}/*.png" ) images.sort() frames = [Image. open (image) for image in images] frame_one = frames[ 0 ] print (frame_one) frame_one.save(framefolder + "/geovindu.gif" , format = "gif" , append_images = frames, save_all = True , duration = 50 , loop = 0 ) def generateQrCode( self ,qrcodestr: str ,qrcodefile: str ): """ 生成二维码 :param qrcodestr: 二要生成的二给码字符 :param qrcodefile: 生成的文件名 :return: """ img = qrcode.make(qrcodestr) type (img) img.save(f '{qrcodefile}.png' ) def readQrCode( self ,qrcodefile: str ): """ 读二维码 :param qrcodefile: 二维码文件名 :return: 返回二维码内容 """ nowpath = os.getcwd() scrimg = Image. open (f "{nowpath}\{qrcodefile}.png" ) m = p.decode(scrimg) codedata = m[ 0 ].data.decode( "utf-8" ) return codedata |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 | nowpath = os.getcwd() f = Common.FileLib.FileHelper() f.Path = nowpath #f.displayFileLsit() #f.traversalFiles() files = f.dirAllFile(nowpath) allf = f.allfiles print ( len (allf)) for ff in allf: for df in ff: if not df = = []: print (df) print (f "文件夹数量:{f.cdir} 文件数量:{f.cfile}" ) |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2014-07-09 csharp: HttpWebRequest and HttpWebResponse