I made it for myself, so I think its use is limited. External files are often read programmatically for configuration, so they are organized for ease of use. By the way, the handling of Datetime is also complicated, so it is summarized. For details on modularization and usage, please refer to the following articles.
Modularize and package your own functions with python --Qiita
MyModule.py
# -*- coding: utf-8 -*-
import time, shutil, os, sys, datetime
class File:
#Read the file and return it as a list (in case of error)"error"return it)
def ReadListOut(path):
try:
with open(path,'r',encoding="utf-8") as f:
Sentence = [s.strip() for s in f.readlines()]
return Sentence
except FileNotFoundError:
return "error"
#Write list type data with line breaks
def WriteListIn(path, Sentence):
Sentence = "\n" + '\n'.join(list(map(str, Sentence)))
with open(path, mode='a',encoding="utf-8") as f:
f.writelines(Sentence)
#Write after breaking a line of text
def WriteStrIn(path, Sentence):
Sentence = "\n" + Sentence
with open(path, mode='a',encoding="utf-8") as f:
f.writelines(Sentence)
#Copy directory or file to any path
def Copy(pathBefore, pathAfter):
if "." in pathBefore:
shutil.copy(pathBefore, pathAfter)
else:
shutil.copytree(pathBefore, pathAfter)
#Move directory or file to any path
def Move(pathBefore,pathAfter):
shutil.move(pathBefore, pathAfter)
#Create a directory in any path
def MakeDir(path):
os.makedirs(path)
#Delete directory or file
def Remove(path):
if "." in path:
os.remove(path)
else:
shutil.rmtree(path)
#Change file modification date
def ChangeUpdateTime(path, timelist): #Year/Month/Day/Time/Minutes/Enter in seconds
time = Define.Date(timelist).timestamp()
os.utime(path, (time, time))
class Program:
#Kill the program
def Exit():
sys.exit()
class Define:
#Create Datetime object from input
def Date(timelist):
return datetime.datetime.strptime("/".join(map(str, timelist)), "%Y/%m/%d")
def Time(timelist):
return datetime.datetime.strptime("/".join(map(str, timelist)), "%H/%M/%S")
def DateTime(timelist):
return datetime.datetime.strptime("/".join(map(str, timelist)), "%Y/%m/%d/%H/%M/%S")
Recommended Posts