1

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
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Ivaldir
  • 185
  • 12

2 Answers2

2

In Windows, CON is a special file. If memory serves, it always exists everywhere with any file extension. This is a holdover from DOS. I can't remember what it's for but I think it was for low level console I/O. It's been a while. Retrocomputing Exchange could probably tell you more than you wanted to know about this.

I think if you name the file pretty much anything else (except PRN or NUL or a few others) your script will behave as expected.

More info on the special files: https://superuser.com/a/613335

And: https://en.wikipedia.org/wiki/Device_file#MS-DOS

wberry
  • 18,519
  • 8
  • 53
  • 85
0

The latter assumption is incorrect due to your string formatting -

'crypto/{}'.format(ticker) is 'crypto/QTCON.csv' if and only if ticker='QTCON.csv'

If CON.csv returns false positive than it prints

CON.csv exists

and proceeds to this block:

with open('crypto/{}.csv'.format(ticker), 'a', newline='') as file:
    writer = csv.writer(file)
    writer.writerow([price, vol24h, mCap, supply, change1h, change1d, change7d, change1m])

Which isn't really related to the upper block. Anyhow, try changing it to this:

with open('crypto/{}.csv'.format(ticker), 'a+', newline='') as file:
    writer = csv.writer(file)
    writer.writerow([price, vol24h, mCap, supply, change1h, change1d, change7d, change1m])

Checkout file open permissions here.

  • I've tried using a+ but this gives the same error, thanks for confirming that it's not due to a file with a similar name. – Ivaldir Dec 15 '19 at 16:40
  • `'crypto/{}'.format(ticker)` this is not in the code in the question. – snakecharmerb Dec 15 '19 at 16:40
  • @snakecharmerb it is in the os.path.exists() function –  Dec 15 '19 at 16:42
  • 2
    @Ivaldir Please note that a+ creates non-existing file and appending to the end of an existing file. Neither way, the problem might be due to the specific file name 'CON' disregarding it's extension. Read this https://fossbytes.com/windows-reserved-folder-con-create/. –  Dec 15 '19 at 16:47