티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
Basic http file downloading and saving to disk in python?
파이썬에서 파일 다운로드와 디스크에 저장하는 기본적인 방법
문제 내용
I've been going through the Q&A on this site, for an answer to my question. However, I'm a beginner and I find it difficult to understand some of the solutions. I need a very basic solution.
이 사이트에서 질문에 대한 답변을 찾고 있습니다. 하지만 저는 초보자이기 때문에 일부 해결책을 이해하기 어렵습니다. 매우 기본적인 해결책이 필요합니다.
Could someone please explain a simple solution to 'Downloading a file through http' and 'Saving it to disk, in Windows', to me?
누군가 "http를 통해 파일 다운로드"와 "Windows에서 디스크에 저장"하는 간단한 해결책을 설명해주실 수 있나요?
I'm not sure how to use shutil and os modules, either.
또한 shutil와 os 모듈을 사용하는 방법도 잘 모르겠습니다.
The file I want to download is under 500 MB and is an .gz archive file.If someone can explain how to extract the archive and utilise the files in it also, that would be great!
다운로드하려는 파일은 500MB 이하의 .gz 압축 파일입니다. 이 파일에서 압축을 푸는 방법과 그 안의 파일을 활용하는 방법도 알려주시면 좋겠습니다!
Here's a partial solution, that I wrote from various answers combined:
여기 몇 가지 답변에서 조합한 부분적인 해결책이 있습니다.
import requests
import os
import shutil
global dump
def download_file():
global dump
url = "http://randomsite.com/file.gz"
file = requests.get(url, stream=True)
dump = file.raw
def save_file():
global dump
location = os.path.abspath("D:\folder\file.gz")
with open("file.gz", 'wb') as location:
shutil.copyfileobj(dump, location)
del dump
Could someone point out errors (beginner level) and explain any easier methods to do this?
초보자 수준의 오류를 지적하고 더 쉬운 방법을 설명해주실 수 있나요?
높은 점수를 받은 Solution
A clean way to download a file is:
파일을 깔끔하게 다운로드하는 방법은 다음과 같습니다:
import urllib
testfile = urllib.URLopener()
testfile.retrieve("http://randomsite.com/file.gz", "file.gz")
This downloads a file from a website and names it file.gz
. This is one of my favorite solutions, from Downloading a picture via urllib and python.
이는 웹 사이트에서 파일을 다운로드하고 file.gz라는 이름으로 저장합니다. 이것은 Downloading a picture via urllib and python에서 제일 좋아하는 해결책 중 하나입니다.
This example uses the urllib
library, and it will directly retrieve the file form a source.
이 예제는 urllib 라이브러리를 사용하며, 직접 소스에서 파일을 가져올 것입니다.
가장 최근 달린 Solution
import urllib.request
urllib.request.urlretrieve("https://raw.githubusercontent.com/dnishimoto/python-deep-learning/master/list%20iterators%20and%20generators.ipynb", "test.ipynb")
downloads a single raw juypter notebook to file.
단일 raw juypter 노트북을 파일로 다운로드합니다.
출처 : https://stackoverflow.com/questions/19602931/basic-http-file-downloading-and-saving-to-disk-in-python
'개발 > 파이썬' 카테고리의 다른 글
판다스 데이터프레임이 비어 있는지 확인하는 방법 (0) | 2023.01.09 |
---|---|
파이썬에서 리스트의 맨 앞에 정수 추가하기 (0) | 2023.01.08 |
Python에서 상대 경로로 파일 열기 (0) | 2023.01.07 |
판다스로 잘못된 라인이 있는 csv 파일 불러오기(오류라인 건너뛰기) (0) | 2023.01.06 |
리스트의 모든 숫자 문자열을 int로 변환하기 (0) | 2023.01.06 |