Modules of frequently used functions in Python (such as reading external files)

at first

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

module

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

Modules of frequently used functions in Python (such as reading external files)
8 Frequently Used Commands in Python Django
Handling of JSON files in Python
Summary of frequently used commands in matplotlib
Output tree structure of files in Python
[python] Frequently used techniques in machine learning
Output formatted output in Python, such as C / C ++ printf.
Summary of how to import files in Python 3
List of frequently used built-in functions and methods
Utilization of recursive functions used in competition pros
Reading and writing CSV and JSON files in Python
Summary of evaluation functions used in machine learning
I want to get / execute variables / functions / classes of external files from Python
The story of reading HSPICE data in Python
Summary of frequently used Python arrays (for myself)
Get files, functions, line numbers running in python
Astro: Python modules / functions often used for analysis
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
Products (iterators) such as maps of Python3 ・ Generator inclusion notation in parentheses is disposable
Note the frequently used options in Python + Selenium + Chrome
A collection of code often used in personal Python
Existence check of external command in Python (like `which`)
Include and use external Kv files in Python Kivy
A collection of commands frequently used in server management
List of Python code used in big data analysis
Comparison table of frequently used processes of Python and Clojure
A collection of Excel operations often used in Python
List of python modules
# 4 [python] Basics of functions
python external file reading
[Python] Reading CSV files
[Scientific / technical calculation by Python] List of usage of (special) functions used in physics by using scipy
[Memo] The mystery of cumulative assignment statements in Python functions
Comparison of how to use higher-order functions in Python 2 and 3
Summary of how to write .proto files used in gRPC
A set of script files that do wordcloud in Python3
[Python] Easy reading of serial number image files with OpenCV
Note installing modules such as pytorch and opencv with pip in Blender python (2.82a or later)
Execute external command in python
Equivalence of objects in Python
Overriding library functions in Python
Reading .txt files with Python
Frequently used subpackages of SciPy
Python frequently used code snippets
Frequently used commands in virtualenv
External command execution in Python
Implementation of quicksort in Python
Python functions learned in chemoinformatics
How to use functions in separate files Perl and Python versions
33 strings that should not be used as variable names in python
Investigating what could be used as a Markdown parser in Python
A class that summarizes frequently used methods in twitter api (python)
Get a list of files in a folder with python without a path