''' Data class'''
import os.path
from random import Random
class TestData(object, Random):
def FetchDataFromFile(self, filename):
""" Open the file as read only """
myfile = open(os.path.join(os.getcwd(),filename), 'r')
""" read the information in the file """
lines = myfile.read()
''' Remove the header as this will not be used '''
header = lines[0]
lines.remove(header)
return lines
我得到了:
ImportError:没有名为path
的模块文件“ pyclasspath /Lib/Testdata.py”,第2行,
os.path正在我项目中的所有其他类中工作。有人能指出我在做什么错误吗?
我将此文件从一个目录移动到另一个目录。除此之外,这个班级和其他班级没有区别。
答案 0 :(得分:3)
import os
应该可以正常工作
import os
from random import Random
class TestData(object, Random):
def FetchDataFromFile(self, filename):
""" Open the file as read only """
myfile = open(os.path.join(os.getcwd(),filename), 'r')
""" read the information in the file """
lines = myfile.read()
''' Remove the header as this will not be used '''
header = lines[0]
lines.remove(header)
return lines
作为帮助你的方法可能
def FetchDataFromFile(self, filename):
""" Open the file as read only """
return list(open(os.path.join(os.getcwd(),filename), 'r'))[1:]
答案 1 :(得分:2)
我将项目中的包名称设为'Lib',并将Testdata模块移动到Lib中。 我猜Python不喜欢包名。将其重命名为Library,现在正在工作。该错误与import语句无关。从os导入路径导入os.path AND都可以正常工作。