0

I'm trying to build an app off the idea of SeaofBTCapp (Sentdex), and Python/Tkinter does not find my "sampleData.csv" file despite being sure I have the right directory. (I've tried .csv and .txt extensions to no avail.) I hope this MWE is not too minimal, obviously I import tkinter and other matplotlib modules, and I from my understanding, the open() function is native to python.

Function:

import matplotlib.animation as animation

def animate(i):
    pullData = open('sampleData.csv', "r").read()
    dataList = pullData.split('\n')
    xList =[]
    yList = []
    for eachLine in dataList:
        if len(eachLine)>1:
            x, y = eachLine.split(',')
            xList.append(int(x))
            yList.append(int(y))
    a.clear()
    a.plot(xList, yList )

Traceback:

File "/Users/nrsmoll/PycharmProjects/SeaofStats/main.py", line 27, in animate
    pullData = open('sampleData.csv', "r").read()
FileNotFoundError: [Errno 2] No such file or directory: 'sampleData.csv'

File system checks:

print(os.getcwd())
/Users/nrsmoll/PycharmProjects/SeaofStats
print(os.listdir())
['.DS_Store', 'main.py', 'sampleData.csv', '.idea']
print(os.listdir())
['.DS_Store', 'sampleData.csv', 'main.py', '.idea']
print(os.path.exists(r"sampleData.csv")) 
True
Nicolas
  • 374
  • 4
  • 18
  • Thanks, that was some last minute attempted debugging. – Nicolas Jul 13 '18 at 08:38
  • 1
    For debugging purposes, can you put os.getcwd() and os.listdir() in your code right before opening the file? Then maybe, you can use os.path.join with os.getcwd and filename. – Lafexlos Jul 13 '18 at 08:40

1 Answers1

0

Argh, the answer is simply to put the WHOLE or absolute path:

pullData = open('/Users/nrsmoll/PycharmProjects/SeaofStats/sampleData.csv', "r").read()

So simple. But if anyone knows how to make it so I don't have to put the absolute path in there, that would be great, I suspect that will be needed when making a standalone project.

Nicolas
  • 374
  • 4
  • 18
  • Ideally, you'd ask the user which file they want to read. There's a file chooser built into Tkinter. Present the file chooser, wait for user to chose file, then fetch the absolute path to the file selected. Check out this question: https://stackoverflow.com/questions/28373288/get-file-path-from-askopenfilename-function-in-tkinter – Plasma Jul 13 '18 at 09:07
  • You shouldn't _have_ to put the absolute path. Most likely, the current working directory has changed, or isn't what you assume it is. Relative paths should work, if you're cwd is where you think it is. – Bryan Oakley Jul 13 '18 at 11:45