【问题标题】:Importing file from path从路径导入文件
【发布时间】:2019-02-26 15:21:07
【问题描述】:

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

FileNotFoundError: [Errno 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')

【问题讨论】:

  • 你试过提供access mode吗?在我看来,这应该不是问题。

标签: python-3.x


【解决方案1】:

你能确定你指向的文件的扩展名是正确的吗?你也试过绝对文件路径吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-12
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多