Answer the question
In order to leave comments, you need to log in
How to download google drive file using python?
How to download file from google drive using python. The official documentation has a method that downloads a file by its id. But for some reason it doesn't work. More precisely, the program works, but the file does not appear in the directory in which the file is located. Help me figure out where it is downloaded and whether it is downloaded at all? And if it does not download, then tell me how to download it?
Here is the code I am using. I got the file id and it is correct. The service class instance was created correctly, other methods worked.
file_id = '1bqLncGocPTak1-1LZu3CPI1WJU-W4MQy'
request = service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
Answer the question
In order to leave comments, you need to log in
If you just download the file via a direct link.
import requests
url = "https://drive.google.com/uc?id=1UCKuSaZ6zDg-NA9LxLspErVw0y2Qjh3Y"
headers = {"user-agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"}
def main():
client = requests.Session()
response = client.get(url, headers=headers)
print(response.status_code)
FILE_TYPE = response.headers["content-type"].split("/")[1]
print(FILE_TYPE)
with open(f"video_file.{FILE_TYPE}", "wb") as video_file:
video_file.write(response.content)
if __name__ == '__main__':
main()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question