Python从文件中读取列表

时间:2017-05-07 21:37:51

标签: python macos python-2.7

我试图从文件中对列表进行排序,但我收到此错误:

Traceback (most recent call last):
  File "/Users/MacbookPro/Documents/Faculta/alg sortare pyth/bubble.py", line 13, in <module>
    f = file.open('lista.txt', 'r')
AttributeError: type object 'file' has no attribute 'open'

这是我的代码:

from timeit import default_timer as timer
import resource
start = timer()
def bubbleSort(alist):
    for passnum in range(len(alist)-1,0,-1):
        for i in range(passnum):
            if alist[i]>alist[i+1]:
                temp = alist[i]
                alist[i] = alist[i+1]
                alist[i+1] = temp


f = file.open('lista.txt', 'r')
long_string = f.readline()
my_list = long_string.split(',')
bubbleSort(alist)
print(alist), resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1000
end = timer()
print(end - start)

1 个答案:

答案 0 :(得分:0)

替换此行:

f = file.open('lista.txt', 'r')

这一个:

f= open('lista.txt', 'r')