I'm learning how to use Python and SublimeText to pull financial data from Yahoo. After watching a tutorial video I came up with this code to get the 1 year range of data from Yahoo for AAPL.
import urllib2
import time
stockToPull = 'AAPL'
def pullData(stock):
try:
fileLine = stock+'.txt'
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=1y/csv'
sourceCode = urllib2.urlopen(urlToVisit).read()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine
if len(splitLine) == 6:
if 'value' not in eachLine:
saveFile = open(fileLine,'a')
lineToWrite = eachLine+'\n'
saveFile.write(lineToWrite)
print 'Pulled',stock
print 'sleeping'
time.sleep(5)
except Exception,e:print 'main loop', str(e)
pullData(stockToPull)
I cannot seem the find the 'AAPL.txt' file the code was supposed to create, so I am assuming the file was never created in the first place.
The code executes correctly, but no file.
Suggestions?
Python's
openis, underneath the covers, C'sfopen, which is relative to the current working directory: your current directory when you ran the program.To illustrate you might try:
and then then
cdto wherever you want and runpython /tmp/touch.pyand see thattouched.fileis created... if you have permissions to do so.My guess is that there's some kind of permissions problem with your working directory.