Pythonでよく使うツール自分まとめ
Pythonでよく使うツールを自分用に集めている
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | #!/usr/bin/python3 """ Python tools Python3 OS:Windows or Linux [add submodule] git submodule add https://github.com/garyohosu/PyTool.git git commit -m "add submodule: hello" [update] git submodule foreach git pull [import] from PyTool.PyTool import PyTool DATE VER NAME COMMENT 2020/02/06 0.00 garyo NEW """ import tkinter as tk from tkinter import messagebox import tkinter.simpledialog as sim import datetime import os import glob import subprocess from tkinter import filedialog as tkFileDialog import time class PyTool(): VERSION = "0.00" logFilename = "" #def __init__(self): def addRoot(self): self.root = tk.Tk() self.root.withdraw() def setRoot(self,root): self.root = root #I/O def inputBox(self,msg): tmp = sim.askstring("Input Box",msg,initialvalue = "") return tmp def messageBox(self,msg): messagebox.showinfo("message",msg) #Log def setLogFilename(self,filename): self.logFilename = filename def logPrint(self,s): f = open(self.logFilename,'a') f.write(s) f.close() def logPrintln(self,s): self.logPrint(s + "\n") #Date/Time def getDate(self): return datetime.datetime.now().strftime('%Y/%m/%d') def getTime(self): return datetime.datetime.now().strftime('%H:%M:%S') def timer(self): return (time.time()) def waitt(self,s): time.sleep(s) #String def left(self,text, n): return text[:n] def right(self,text, n): return text[-n:] def mid(self,text, n, m): return text[n-1:n+m-1] #File def appPath(self): return (os.getcwd()) def dir(self,filepath): return (os.path.exists(filepath)) def getPath(self,filepath): return (os.path.dirname(filepath)) def getFilename(self,filepath): return (os.path.basename(filepath)) def pathJoin(self,path,filename): return (os.path.join(path,filename)) def makeDir(self,filename): if os.path.exists(filename) == False: os.makedirs(os.path.dirname(filename),exist_ok = True) def getFileList(self,path,filename): #print("glob.glob(" + self.pathJoin(os.path.dirname(path) + "/**/",filename) + ")") return (glob.glob(self.pathJoin(os.path.dirname(path) + "/**/",filename), recursive=True)) def openFileDialog(self,iDir = "."): #fTyp=[('Text File','*.txt')] fTyp=[('DAT FILE','*.dat'),('ALL FILE','*.*')] #iDir='/home/garyo' #iDir='c:/' #Windows #askopenfilename filename = tkFileDialog.askopenfilename(filetypes=fTyp,initialdir=iDir) return (filename) def openFolderDialog(self,iDir = "."): dirname = tkFileDialog.askdirectory(initialdir=iDir) return (dirname) #shell def shell(self,cmd): return(subprocess.getoutput(cmd)) class TextFile: def __init__(self): self.filename = "" self.lines = [] def clear(self): self.lines = [] def count(self): return (len(self.lines)) def items(self,n): return (self.lines[n]) def add(self,d): self.lines.append(d) def csvRead(self,s,x): v=[] v = s.split(',') return v[x] def itemsXY(self,x,y): return csvRead(self,self.lines[y],x) def read(self): self.clear() return (self.readAppend()) def write(self): if os.path.exists(self.filename) == False: os.makedirs(os.path.dirname(self.filename),exist_ok = True) f = open(self.filename,'w') f.writelines(self.lines) f.close() def printlines(self): for line in self.lines: print(line) def readAppend(self): if os.path.exists(self.filename) == True: f = open(self.filename,'r') self.lines = f.readlines() f.close() return (True) else: return (False) def writeAppend(self): if os.path.exists(self.filename) == False: os.makedirs(os.path.dirname(self.filename),exist_ok = True) f = open(self.filename,'a') f.writelines(self.lines) f.close() def writeAppend1Line(self,msg): mode = "a" if os.path.exists(self.filename) == False: os.makedirs(os.path.dirname(self.filename),exist_ok = True) mode = "w" f = open(self.filename,mode) f.write(msg) f.close() if __name__ == "__main__": pt = PyTool() pt.addRoot() msg = pt.inputBox("test") pt.messageBox(msg) pt.setLogFilename("log.log") pt.logPrintln("test") print(pt.getDate()) print(pt.getTime()) print(pt.left("1234567890",2)) print(pt.right("1234567890",2)) print(pt.mid("1234567890",2,3)) print(pt.appPath()) print(__file__) print(pt.getPath(__file__)) print(pt.getFilename(__file__)) print(pt.dir(__file__)) print(pt.dir(pt.appPath() + "test.txt")) print(pt.pathJoin(pt.appPath() ,"test.txt")) print(pt.makeDir("./test/test.txt")) flist = pt.getFileList(pt.appPath(),"*") for f in flist: print(f) print("py") flist = pt.getFileList(pt.appPath(),"*.py") for f in flist: print(f) print(pt.shell("dir")) print(pt.openFileDialog()) print(pt.openFolderDialog()) print(pt.timer()) pt.waitt(3) print(pt.timer()) |
コメント
コメントを投稿