Reputation: 133
I am a beginner with python and I have started to develop a simple program that will scrape the current bitcoin price from a website every 5 minutes, detect the change from the previous scrape, print this information and also write it into a text file. My current code does all of this, except it fails to write it into the text document, I have no clue why.
I'm pretty sure the error is in the write() function, because that's where the error message points to, but I can include the full code if anyone thinks that that is the issue.
def write():
global change
nps = str(newPrice) + ' USD'
writeThis = nps + ' ' + writeChange
f.write(writeThis)
f.write('\n')
print(writeThis)
with open("bitcoinPrice.txt","w+") as f:
while True:
getCost()
calcChange()
write()
As I'm very new to python, I understand that there are probably much better approaches to a lot of my code, and I'm completely open to suggestions!
Upvotes: 0
Views: 54
Reputation: 396
You need to pass the file object f
to write()
:
def write(f): # <-- file object
global change
nps = str(newPrice) + ' USD'
writeThis = nps + ' ' + writeChange
f.write(writeThis)
f.write('\n')
print(writeThis)
with open("bitcoinPrice.txt","w+") as f:
while True:
getCost()
calcChange()
write(f) # <-- write needs to know what to write to
Upvotes: 1