一道 python 题,大家给帮帮忙, thx!

2016-10-28 00:58:59 +08:00
 Tianny

题目如下: 写一个逐页显示文本文件的程序. 提示输入一个文件名, 每次显示文本 文件的 25 行, 暂停并向用户提示"按任意键继续.", 按键后继续执行

1714 次点击
所在节点    Python
6 条回复
eyp82
2016-10-28 07:20:51 +08:00
随手撸了一个, 代码有点烂, 哪里有 bug 还请各位不吝指出:


#!/usr/bin/env python3


def show_content(file, paginate):
"""Show the content of a file, display 'paginate'
lines and ask user to press any key to continue"""
with open(file) as f:
for idx, line in enumerate(f, start=1):
print(line, end='')
if not idx % paginate:
print('Press any key to continue')
_ = input()


if __name__ == '__main__':
show_content('test.out', 25)
eyp82
2016-10-28 07:22:44 +08:00
缩进都被吃掉了....
Tianny
2016-10-28 11:12:24 +08:00
@eyp82 thx !我再把缩进的给补回来
#!/usr/bin/env python3

def show_content(file, paginate):
"""show the content of a file,display paginate lines
and ask user to press any key to continue"""

with open(file) as f:
for idx, line in enumerate(f, start=1):
print (line,end='')
if not idx % paginate:
print ('Press any key to continue')
_ = input()

if __name__ == '__main__':
show_content('/home/tiantian/.vimrc', 25)
Tianny
2016-10-28 11:13:54 +08:00
@eyp82 ...我的也被吃掉了。。
btjoker
2016-10-28 20:55:36 +08:00
@Tianny 试试全角空格
yexiaoxing
2016-10-29 09:12:31 +08:00
试试 gist …

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/316014

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX