python
-
loggingpython/응용 2022. 5. 22. 00:57
import logging # https://docs.python.org/3/library/logging.html#logrecord-attributes formatter = '%(name)s %(asctime)s %(levelname)s %(message)s' logging.basicConfig(level=logging.INFO, format=formatter) # level 을 지정해 줘야 아래 logger.setLevel 이 적용되는듯? # logging.basicConfig(level=logging.INFO, filename='test.log') logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) h = logging.FileHa..
-
optparsepython/응용 2022. 5. 22. 00:10
import sys print(sys.argv) #################################################################### from optparse import OptionParser if __name__ == '__main__': usage = 'usage: %prog [options] arg1 arg2' parser = OptionParser(usage=usage) parser.add_option('-f', '--file', action='store', type='string', dest='filename', help='file name') # myprog.py -f input.txt parser.add_option('-n', '--num', actio..
-
yaml / configparserpython/응용 2022. 5. 21. 23:26
YAML config https://pypi.org/project/PyYAML/3.10/ Install config.yml DEFAULT: debug: true web_server: host: 127.0.0.1 port: 80 위의 config.yml 파일 생성하기 import yaml with open('config.yml', 'w') as yaml_file: yaml.dump({ 'DEFAULT': { 'debug': True }, 'web_server': { 'host': '127.0.0.1', 'port': 80 } }, yaml_file) 위의 config.yml 파일 읽기 import yaml with open('config.yml', 'r') as yaml_file: config = yaml..
-
datetime / timepython/기본 2022. 5. 2. 14:21
여기서는 standard library 로 제공되는 datetime 을 사용했지만 arrow 의 사용도 고민해 볼 만하다. import datetime now = datetime.datetime.now() print(now) # 2022-05-02 14:09:29.165956 print(now.isoformat()) # 2022-05-02T14:09:29.165956 print(now.strftime('%d/%m/%y-%H%M%S%f')) # 02/05/22-140929165956 today = datetime.date.today() print(today) # 2022-05-02 print(today.strftime('%d/%m/%y')) t = datetime.time(hour=1, minute=10,..
-
tempfile / subprocesspython/기본 2022. 5. 2. 14:04
import tempfile # 임시 디렉토리에서 압축을 푼다든지로 응용될 수 있음 with tempfile.TemporaryFile(mode='w+') as t: t.write('hello') t.seek(0) print(t.read()) # hello with tempfile.NamedTemporaryFile(delete=False) as t: print(t.name) with open(t.name, 'w+') as f: f.write('test\n') f.seek(0) print(f.read()) with tempfile.TemporaryDirectory() as td: print(td) ##############################################################..
-
tar / zippython/기본 2022. 5. 2. 13:39
import tarfile # tar.gz 파일 생성하기 with tarfile.open('test.tar.gz', 'w:gz') as tr: tr.add('test_dir') # tar.gz 파일 압축풀기 with tarfile.open('test.tar.gz', 'r:gz') as tr: tr.extractall(path='test_tar') # 특정 파일의 내용만 볼 때 with tr.extractfile('test_dir/sub_dir/sub_test.txt') as f: print(f.read()) #################################################################### import zipfile with zipfile.ZipFile('test...
-
os, pathlib, glob, shutilpython/기본 2022. 5. 1. 21:31
비슷하게 여러 라이브러리가 있지만 pathlib 를 추천함 import os print(os.path.exists('test.txt')) print(os.path.isfile('test.txt')) print(os.path.isdir('test.txt')) os.rename('test.txt', 'renamed.txt') os.symlink('renamed.txt', 'symlink.txt') os.mkdir('test_dir') os.rmdir('test_dir') # empty dir 일때만 삭제 가능 import pathlib pathlib.Path('empty.txt').touch() # empty file 생성 os.remove('empty.txt') ########################..
-
Template, Csvpython/기본 2022. 5. 1. 21:19
# template import string s = """\ Hi $name. $contents Have a good day """ t = string.Template(s) c = t.substitute(name='Mike', contents='How are you?') print(c) #################################################################### # csv import csv # windows 의 경우 \r\n 때문에 newline 을 '' 로 설정 with open('test.csv', 'w', newline='') as csv_file: fieldnames = ['Name', 'Count'] writer = csv.DictWriter(cs..