ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • datetime / time
    python/기본 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, second=5, microsecond=100)
    print(t.isoformat())  # 01:10:05.000100
    
    now = datetime.datetime.now()
    d = datetime.timedelta(weeks=1)
    print(now - d)  # 1주일 전 시간
    
    ####################################################################
    import time
    
    time.sleep(2)  # 2초 sleep
    print(time.time())  # 1651468537.3310742
    
    import os
    import shutil
    
    file_name = 'test.txt'
    
    # 예제: 파일 백업
    backup_date = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
    if os.path.exists(file_name):
        shutil.copy(file_name, f"{file_name}.{backup_date}")

    'python > 기본' 카테고리의 다른 글

    Python Closure  (0) 2022.10.21
    requirements.txt 자동 생성하기  (0) 2022.05.31
    tempfile / subprocess  (0) 2022.05.02
    tar / zip  (0) 2022.05.02
    os, pathlib, glob, shutil  (0) 2022.05.01

    댓글

Designed by Tistory.