Skip to content

Instantly share code, notes, and snippets.

@hongdonghyun
Created February 10, 2020 10:32
Show Gist options
  • Save hongdonghyun/7dfc98e8190a097beb86fc24eecf025e to your computer and use it in GitHub Desktop.
Save hongdonghyun/7dfc98e8190a097beb86fc24eecf025e to your computer and use it in GitHub Desktop.
from urllib.request import urlopen
# 아래 첨부한 이미지에서 29 ~ 30번째 줄 코드를 작성한 이유가 궁금합니다.
"""
with문은 파일,네트워크,데이터베이스등의 연결을 열고 with안의 구문이 모두 실행되고나면 자동으로 close를 실행하게 해주는 예약어입니다.
파일을 열었으면 당연히 닫아야 하기 때문에 with문으로 작성되었고
파일의 경로에 해당모드로 열겠다 라는 내용이 됩니다.
w는 쓰기모드이며 파일이 없다면 자동으로 생성됩니다.
b는 바이너리파일 모드이며 wb는 바이너파일로 작성하겠다는 내용이됩니다.
바이너리파일은 텍스트의 인코딩이나 줄바꿈문자(\n)에 대한 변환을 하지 않고 1byte단위로 저장하게 됩니다
바이너리파일은 주로 이미지,csv파일을 저장하실때 주로 사용됩니다.
contents에 대한 내용이 이미지 파일과 html파일에 텍스트로 적히는게 가능한지
>>> 이미지파일에 내용을 적는다는게 어떤말인지 잘 모르겠네요.
>>> 제 나름대로 유추해서 이미지를 저장하는 코드를 만들어보았는데 이게 맞나요?
>>> html의 경우 양식만 잘 맞춰서 적어주면 안될건 없어보입니다.
"""
res = urlopen("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png")
with open("./google.png", "wb") as f:
f.write(res.read())
html = """<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
</html>
"""
with open("./section02-1.html", "w") as f:
f.write(html)
@coke05288
Copy link

감사합니다! 이해했습니다 ㅠ.ㅠ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment