Just pass the path of the output file as a second argument to urlretrieve()
. The filename is also the first item of the tuple returned by this function.
import urllib
STOCK_URL = 'http://ichart.finance.yahoo.com/table.csv?s=%s&a=07&b=25&c=2014&d=07&e=29&f=2014&g=d&ignore=.csv'
def pulldata(stock, filename):
return urllib.urlretrieve(STOCK_URL % urllib.quote(stock), filename)
filename, headers = pulldata("RUT", "RUT-data.csv")
If you want to save the file in your documents folder, build the filename like this (there are several ways to do this, but this one will work on desktop Python as well):
import os
from os.path import exists, dirname, join
# create output directory if it doesn't exist yet
documents = join(os.getenv('HOME'), 'Documents')
if not exists(documents):
os.mkdir(documents)
filename = join(documents, 'RUT-data.csv')