
320 Chapter 6
코드
6장의 코드
tasks = open('todos.txt')
for chore in tasks:
print(chore, end='')
tasks.close()
with open('todos.txt') as tasks:
for chore in tasks:
print(chore, end='')
기억하세요. 둘 다 같은 동작을
수행합니다. 하지만 파이썬 프로그래머는
아래쪽이 아니라 오른쪽의 코드를
선호해요.
...
def log_request(req: 'flask_request', res: str) -> None:
with open('vsearch.log', 'a') as log:
print(req.form, req.remote_addr, req.user_agent, res, file=log, sep='|')
...
@app.route('/viewlog')
def view_the_log() -> 'html':
contents = []
with open('vsearch.log') as log:
for line in log:
contents.append([])
for item in line.split('|'):
contents[-1].append(escape(