我對python和flask很陌生。我只是想讀一個CSV文件,但它給了我一個錯誤「FileNotFoundError:[Errno 2]沒有這樣的文件或目錄:'Dog-Data.csv'」,每次我嘗試運行run.py使用Python讀取CSV文件時出錯,Flask
這裏是如果我註釋掉涉及CSV行我的文件順序
DD\
static\
(bunch of image files)
templates\
(bunch of template files)
__init__.py
views.py
Dog-Data.csv
views.py
from flask import render_template
from app import app
import csv
@app.route('/')
@app.route('/Home')
def home():
return render_template("Home.html",title='Home')
@app.route('/MakeaMatch')
def makeamatch():
return render_template("MakeaMatch.html",title='Make a Match!')
@app.route('/Game')
def game():
return render_template("Game.html",title='Game')
@app.route('/ListofDogs')
def listofdogs():
return render_template("ListofDogs.html",title='List of Dogs')
@app.route('/About')
def about():
return render_template("About.html", title='About')
@app.route('/Contact')
def contact():
return render_template("Contact.html", title='Contact')
@app.route('/MatchResult')
def matchresult():
class DogBreed:
def __init__(self, br, per, si, lif, hou, cli):
self.breed = br
self.personality = per
self.size = si
self.lifestyle = lif
self.housing = hou
self.climate = cli
self.match = 0
#List that will contain class DogBreed
ListOfBreeds = []
data_file = open('Dog-Data.csv')
csv_file = csv.reader(data_file)
for row in csv_file:
#print (row) #will print the csv file
#print (row[2]) #will print element of that row
dog_breed = DogBreed(row[0],row[1].lower().split(", "),row[2].lower(),row[3].lower(),row[4].lower(),row[5].lower())
ListOfBreeds.append(dog_breed)
data_file.close()
#MORE CODES HERE. OMITTED BECAUSE I DON'T THINK IT'S RELEVANT
return render_template("MatchResult.html",title='Match Result',posts=ListOfBreeds)
加載網頁和模板,顯示了罰款。但是,它當然不會顯示我想要的結果。
我也試圖把狗-Data.csv到靜態文件夾,並用
data_file = open('static/Dog-Data.csv')
但是這也不能工作。
非常感謝您的幫助。
謝謝。我忽略了一件很簡單的事情。 – key