Python 3.6/Tkinter: 找不到文件 -> FileNotFoundError: [Errno 2] 没有这样的文件或目录: 'XXXX'



我正试图根据SeabBTApp(Sendex(的想法构建一个应用程序,尽管我确信自己有正确的目录,但Python/Tkinter找不到我的"sampleData.csv"文件。(我尝试过.csv和.txt扩展,但都没有用。(我希望这个MWE不要太小,显然我导入了tkinter和其他matplotlib模块,据我所知,open((函数是python的原生函数。

功能:

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 )

追溯:

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'

文件系统检查:

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

Argh,答案是简单地放置完整或绝对路径:

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

这么简单。但如果有人知道如何制作它,这样我就不必把绝对的路径放在那里,那就太好了,我怀疑在制作一个独立项目时需要这样做。

相关内容

最新更新