I'm doing some small scale projects with Python to learn it a bit, one of those is writing & reading from .csv files. I wrote a script that should check wether a file exists and if no create one with the correct header, then place the data within it. And if it does exist it should add the data onto the existing .csv file. Here's the code snippet:
if not os.path.exists('crypto/{}.csv'.format(ticker)):
with open('crypto/{}.csv'.format(ticker), 'w', newline='') as file:
writer = csv.writer(file)
logger.logChanges('{} ({}) added'.format(name, ticker), True)
writer.writerow(['price', 'vol24h', 'mCap', 'supply', 'change1h', 'change1d', 'change7d', 'change1m'])
else:
print('{} exists'.format(ticker))
with open('crypto/{}.csv'.format(ticker), 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow([price, vol24h, mCap, supply, change1h, change1d, change7d, change1m])
This goes fine until it's asked to check wether CON.csv exists (which isn't the case), it says the file exists and goes to the following bit trying to write to a file that doesn't exist. It then gives the following error:
ValueError: Must have exactly one of read or write mode
From what I understand this means it's trying to both read and write? I've looked into the folder and CON.csv isn't in there, there is however a file named QTCON.csv could it be that this is the reason for the false positive? And how do I fix this?
Edit:
Traceback (most recent call last):
File "c:/Users/lennart/Desktop/PythonProject/.vscode/crypto.py", line 62, in <module>
grabInfo()
File "c:/Users/lennart/Desktop/PythonProject/.vscode/crypto.py", line 55, in grabInfo
with open('crypto/{}.csv'.format(ticker), 'a', newline='') as file:
ValueError: Must have exactly one of read or write mode