Pythonでよく使うツール自分まとめ
Pythonでよく使うツールを自分用に集めている
#!/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())
コメント
コメントを投稿