Python Forum

Full Version: download file from google drive ..
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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())
(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
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)
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
(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 ?
(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.
THANK U very much i understand now :)