download a file to a given path

Posted on
Fri Mar 30, 2018 10:39 am
haavarda offline
User avatar
Posts: 640
Joined: Aug 18, 2012
Location: Norway

download a file to a given path

Is there anyone that know of a script that will download a file in a url to a location on the mac?
I have a url that contains a mp3, and I want to store that mp3 file in a folder on my mac. If there already is a file there, I want to replace it.

Håvard

Posted on
Fri Mar 30, 2018 10:48 am
Colorado4Wheeler offline
User avatar
Posts: 2773
Joined: Jul 20, 2009
Location: Colorado

Re: download a file to a given path

You could try this (not knowing your exact circumstances, just assumptions):
Code: Select all
import os
import requests

url = "http://your_file_location_url"
path = "/users/myloginname/Documents/stuff i downloaded"
data = requests.get(url).content
if os.path.exists (path):
   with open(path + "/filename.txt", 'w') as file_:
      file_.write (data)
      
   file_.close()

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Fri Mar 30, 2018 11:14 am
jay (support) offline
Site Admin
User avatar
Posts: 15303
Joined: Mar 19, 2008
Location: Austin, Texas

Re: download a file to a given path

FYI, the close() call is unnecessary (and I think as written it may actually throw an exception) since you opened it inside a with block. Fun fact which I didn't know until recently, you can actually open multiple files in the same with statement:

Code: Select all
with open(newfile, 'w') as outfile, open(oldfile, 'r', encoding='utf-8') as infile:

And once the block ends all files will be closed automatically.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Mar 30, 2018 11:19 am
haavarda offline
User avatar
Posts: 640
Joined: Aug 18, 2012
Location: Norway

Re: download a file to a given path

Thanks. That worked exactly as I was hoping. The first one that is. :)
It also works when I remove the close part...

Håvard

Posted on
Fri Mar 30, 2018 1:10 pm
Colorado4Wheeler offline
User avatar
Posts: 2773
Joined: Jul 20, 2009
Location: Colorado

Re: download a file to a given path

jay (support) wrote:
FYI, the close() call is unnecessary

Old school techniques still prevail. :D

jay (support) wrote:
(and I think as written it may actually throw an exception) since you opened it inside a with block

It shouldn't because the variable is declared and alive, I use this method most of the time without error. Of course if I don't need Close() then it's not needed anyway.

jay (support) wrote:
Fun fact which I didn't know until recently, you can actually open multiple files in the same with statement

That's pretty cool. There's a lot of those shortcuts I've never taken the time to brush up on because I have such seldom need for them.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests