10.5 연습문제

10.1

from datetime import date
date_str = date.strftime(date.today(), "%Y년 %m월 %d일")
with open('today.txt', 'wt') as f:
f.write(date_str)

10.2

today_string = 'asd'
with open('today.txt', 'rt') as f:
today_string = f.read()
print(today_string)

2018년 03월 08일

10.3

import datetime
today_date = datetime.datetime.strptime(today_string, '%Y년 %m월 %d일')
today_date.year

2018

today_date.month

3

today_date.day

8

10.4

import os
os.listdir('.')

[‘ohwell.txt’, ‘poems’, ‘10.2 디렉터리.ipynb’, ‘10.3 프로그램과 프로세스.ipynb’, ‘10.5 연습문제.ipynb’, ‘jeepers.txt’, ‘.ipynb_checkpoints’, ‘today.txt’, ‘10.4 달력과 시간.ipynb’, ‘10.1 파일.ipynb’, ‘yikes.txt’]

10.5

os.listdir('../')

[‘9. 웹’, ‘.idea’, ‘10. 시스템’, ‘3. 파이채우기: 리스트, 튜플, 딕셔너리, 셋’, ‘.ipynb_checkpoints’, ‘6. 객체와 클래스’, ‘intro’, ‘5. 파이 포장하기: 모듈, 패키지, 프로그램’, ‘7. 데이터 주무르기’, ‘4. 파이 크러스트: 코드 구조’, ‘8. 흘러가는 데이터’]

10.6

import multiprocessing
import os
def do_this(what):
whoami(what)

def whoami(what):
print('Process %s says %s' % (os.getpid(), what))

if __name__ == '__main__':
whoami("I'm the main program")
for n in range(4):
p = multiprocessing.Process(target=do_this, args=("I'm function %s" % n,))
p.start()
import multiprocessing
import time
def show_time():
import time
import random
time.sleep(random.randrange(5))
print(datetime.datetime.now())

for n in range(5):
p = multiprocessing.Process(target=show_time)
p.start()

2018-03-08 10:25:56.371575 2018-03-08 10:25:58.370778 2018-03-08 10:25:59.368602 2018-03-08 10:25:59.365596 2018-03-08 10:26:00.363403

10.7

from datetime import date
birth_date = date(1986, 2, 3)
print(birth_date)

1986-02-03

10.8

print(datetime.datetime.strftime(birth_date, '%A'))

Monday

10.9

from datetime import timedelta
future = birth_date + timedelta(days=10000)
future

datetime.date(2013, 6, 21)

Comments