Issue
sav = []
def fileKeep(sav):
classA = open("classA", "r")
for line in classA:
sav.append(line.split())
file.close()
return
fileKeep(sav)
This is the end of my code. I get a File Not Found error that I do not get anywhere else, even though I have used the file nearer to the beginning of the code as well. Any assistance is welcome, thanks.
Solution
You code is assuming that the current working directory is the same as the directory your script lives in. It is not an assumption you can make.
Use an absolute path for your data file. You can base it on the absolute path of your script:
import os.path
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
class_a_path = os.path.join(BASE_DIR, "classA")
classA = open(class_a_path)
You can verify what the current working directory is with os.getcwd()
if you want to figure out where instead you are trying to open your data file.
Your function could be simplified to:
def fileKeep(sav):
with open(class_a_path) as class_:
sav.extend(l.split() for l in class_)
provided class_a_path
is a global.
Answered By - Martijn Pieters
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.