Bottom Page

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
Quote
#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())
Quote
#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
Quote
#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)
Quote
#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
Quote
#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 ?
Quote
#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.
Quote
#8
THANK U very much i understand now :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Nested Subdirectory within current sub on shared network drive Agregware 1 340 Jun-19-2019, 10:07 PM
Last Post: Gribouillis
  trouble with os.listdir on a network drive lconner 10 1,594 Jun-04-2019, 07:16 PM
Last Post: DeaD_EyE
  Download entire web pages and save them as html file with urllib.request fyec 2 3,785 Jul-13-2018, 10:12 AM
Last Post: Larz60+
  download dataset from SH file Felix 1 749 Mar-28-2018, 05:04 AM
Last Post: Larz60+
  Best Module for Navigating Google Drive? HiImNew 2 892 Nov-29-2017, 01:38 AM
Last Post: HiImNew
  Download file from Internet Tribunal 1 1,157 Oct-11-2017, 06:03 PM
Last Post: Larz60+
  Uploading CSV Files to Google drive in a specified folder prashant8530 1 1,254 Sep-30-2017, 10:58 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)