从路径导入文件

时间:2019-02-26 04:52:02

标签: python-3.x

我正在编写一个从文件'point.dat'读取的python程序。但是,当我运行代码时,收到以下错误消息。

  

FileNotFoundError:[错误2]没有这样的文件或目录:'points.dat'

我可以知道缺少什么吗?我尝试用文件路径替换,但效果不佳。我需要导入任何库才能使其正常工作吗?

xy=0
xx=0
with open('points.dat') as f:
    for line in f:
        x,y=[float(p.strip()) for p in line.split(',')]
        xy+=x*y
        xx+=x*x

k=xy/xx

print('Equation of line y = {:.2f}x'.format(k))
x=input('Enter x coordinate or <ENTER> to end: ')
while x!='':
    y=float(input('Enter y coordinate: '))
    x=float(x)
    if(abs(x*k-y)<1e-2):
        print('('+str(x)+',',str(y)+') is on the fitted line')
    else:
        print('('+str(x)+',', str(y) + ') is not on the fitted line')
    x = input('Enter x coordinate or <ENTER> to end: ')
print('End Program')

1 个答案:

答案 0 :(得分:0)

您能确定扩展名对于您指向的文件是正确的吗?还尝试过绝对文件路径吗?