Python Forum
download file from google drive ..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
download file from google drive ..
#1
hello all ....
im trying to download a file from a direct link ( google drive ) ...
this is my function ..

command = raw_input()
linux = os.getenv("HOME")

def downloadfile():
    
    if os.path.isfile(linux + "/plink.exe"):
        pass
    else:
        urllib.urlretrieve(command, str(linux) + '/plink.exe')
it will take the link from command vir ... but i need to download it without writing the filename !! i mean i need it to do it auto ... how i can do this in python ??
this is my link : https://drive.google.com/uc?export=downl...-jD8xXvK3m
Reply
#2
import os
import urllib.request

linux = os.getenv("HOME")
outfile = linux  + "/Downloads/plink.exe"
 
DLFile = urllib.request.urlopen("https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m")
with open(outfile,'wb') as out:
    out.write(DLFile.read())
Reply
#3
(Sep-19-2018, 09:40 AM)Axel_Erfurt Wrote:
import os
import urllib.request

linux = os.getenv("HOME")
outfile = linux  + "/Downloads/plink.exe"
 
DLFile = urllib.request.urlopen("https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m")
with open(outfile,'wb') as out:
    out.write(DLFile.read())

thank u but u did not understand what i want !! i dont want to write the ouput name ( /Downloads/plink.exe" ) i need to download without write filename in the code ... need the code to bring it from the link and save it to home dir
Reply
#4
ok then use wget module

import wget
import os
from os.path import expanduser

myhome = expanduser("~")
### set working dir
os.chdir(myhome)


url = "https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m"
print('downloading ...')
wget.download(url)
Reply
#5
Can use wget package as postet bye @Axel_Erfurt.

If doing it yourself have to look at file header,as url address dos not give away filename or extension.
For this use Requests,or always use Requests and not urllib.
headers['Content-Disposition'] gives info about filename,but the sting is a little messy so have to parse out file name.
import requests
import re

url = 'https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m'
response =  requests.get(url)
header = response.headers['Content-Disposition']
file_name = re.search(r'filename="(.*)"', header).group(1)
with open(file_name,'wb') as f:
    f.write(response.content)
This give same as wget module Copy of plink.exe.
Can aslo do this with open(file_name.split()[-1],'wb') as f: to just get plink.exe
evilcode Wrote:command = raw_input()
No and no use Python 3.6 --> Cool
Reply
#6
(Sep-19-2018, 11:55 AM)snippsat Wrote:
file_name = re.search(r'filename="(.*)"', header).group(1)

thank u very much it's working ... but i did not understand the above line can u explain what it do in details ?
Reply
#7
(Sep-21-2018, 04:16 PM)evilcode1 Wrote: thank u very much it's working ... but i did not understand the above line can u explain what it do in details ?
I use regex .
The string get back is messy.
>>> header = response.headers['Content-Disposition']
>>> print(header)
attachment;filename="Copy of plink.exe";filename*=UTF-8''Copy%20of%20plink.exe
So can use regex take out only stuff wanted.

A other example:
>>> import re
>>> 
>>> s = 'aaaa some text foo="bar" [bbbb]'
>>> r = re.search(r'foo=\"(.*)\"\s\[(.*)\]', s)
>>> r.group(0)
'foo="bar" [bbbb]'
>>> r.group(1)
'bar'
>>> r.group(2)
'bbbb'
So i can take out part i want from string,regex is a big topic so look into tutorials and documentation.
Reply
#8
THANK U very much i understand now :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  FTP Download of Last File jland47 4 291 Mar-16-2024, 09:15 AM
Last Post: Pedroski55
Question How to get a removable disc type in drive Daring_T 12 971 Feb-11-2024, 08:55 AM
Last Post: Gribouillis
  open python files in other drive akbarza 1 632 Aug-24-2023, 01:23 PM
Last Post: deanhystad
  Integrating Google Drive in App Lahearle 0 440 Jul-19-2023, 05:51 PM
Last Post: Lahearle
  download a file from a URL JayManPython 7 1,242 Jun-28-2023, 07:52 AM
Last Post: JayManPython
  code to send attachments contained on the drive. stefanoste78 1 822 Oct-12-2022, 02:16 AM
Last Post: Larz60+
  access is denied error 5 for network drive mapping ? ahmedbarbary 2 1,731 Aug-17-2022, 10:09 PM
Last Post: ahmedbarbary
  Cant Access D: Drive kucingkembar 4 1,265 Jul-28-2022, 04:53 PM
Last Post: kucingkembar
  Deploy Python to Cloud and save output to Google Drive chandrabr80 2 1,524 Jan-25-2022, 06:56 AM
Last Post: ndc85430
  Calling exe on network drive GrahamL 4 2,468 Jan-21-2022, 12:01 PM
Last Post: GrahamL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020