0

I'm trying to download a pdf using a google-drive link but my PC is denying me access when I try to store it I want the code to go on that url and open the file and download it. How do I make it grant permission to store the data. I gave tried changing the directories and changing the security settings of the folder but it does not work

import requests

def download_file_from_google_drive(id, destination):
    URL = "https://docs.google.com/uc?export=download"

    session = requests.Session()

    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)

    if token:
        params = { 'id' : id, 'confirm' : token }
        response = session.get(URL, params = params, stream = True)

    save_response_content(response, destination)    

def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value

    return None

def save_response_content(response, destination):
    CHUNK_SIZE = 32768

    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)

if __name__ == "__main__":
    file_id = '1S6OuauRAYD6Ts7InfF8uEL4D6U_HeK1t'
    destination = "C:/Users/PRATHAMESH/"
    download_file_from_google_drive(file_id, destination)

But it is throwing an error

PermissionError                           Traceback (most recent call last)
<ipython-input-9-c34c9d27dda6> in <module>
     33     file_id = '1S6OuauRAYD6Ts7InfF8uEL4D6U_HeK1t'
     34     destination = "C:/Users/PRATHAMESH/"
---> 35     download_file_from_google_drive(file_id, destination)

<ipython-input-9-c34c9d27dda6> in download_file_from_google_drive(id, destination)
     13         response = session.get(URL, params = params, stream = True)
     14 
---> 15     save_response_content(response, destination)
     16 
     17 def get_confirm_token(response):

<ipython-input-9-c34c9d27dda6> in save_response_content(response, destination)
     25     CHUNK_SIZE = 32768
     26 
---> 27     with open(destination, "wb") as f:
     28         for chunk in response.iter_content(CHUNK_SIZE):
     29             if chunk: # filter out keep-alive new chunks

PermissionError: [Errno 13] Permission denied: 'C:/Users/PRATHAMESH/'
PRATHAMESH
  • 123
  • 2
  • 3
  • 11

1 Answers1

0

If you check the 'C:/Users/PRATHAMESH/' folder, you will probably see this in the folder properties:

user folder properties in windows 10.

This means that this folder is protected from any "normal" applications trying to write to it.

The simplest way to avoid it is to run your python application (or terminal application) with administrator privileges (right click -> Run as Administrator)

You can also try removing write protection from the folder (i am not entirely sure that windows would let you do that to your user's profile!) or you can, and probably should, simply change the target directory to a non-system directory that would not have such strict access protection

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35