预期的str字节或类os.pathlike对象不是buildin_function_or_method

时间:2019-06-03 03:21:16

标签: python

只想计算关于气泡排序程序的单个文件的所有代码行,并衡量其自身的生产率。我有一个应该执行此操作的方法,但是当我在控制台上运行程序时出现 这是我的代码:

from timeit import default_timer as timer

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

alist = [2, 5, 10, 3, 1, 4]
bubblesort(alist)
print(alist)
end = timer()

time= end - start
print("The excecution time is: " + str(time))

performance= 1/time

print("The performance: " + str(performance))

def count_lines(filename):
    with open(filename) as f:
        cnt = sum(1 for line in f)
        print(f'There are {cnt} lines in {filename}')

print(count_lines(timer))
"Traceback (most recent call last): 
File ".\timer.py", line 32, in <module> print(count_lines(timer)) 
File ".\timer.py", line 28, in count_lines 
with open(filename) as f: TypeError: expected str, bytes or os.PathLike object, not builtin_function_or_method".

1 个答案:

答案 0 :(得分:1)

timer变量应包含类似于字符串的路径,这是打开文件对象所必需的。但是,您传递的是内置方法timer。因此,将参数更改为文件的路径,代码就可以正常工作。