如何在python中打开一个文本文件,只将第一个可见部分写入屏幕?

2024-06-16 11:00:31 发布

您现在位置:Python中文网/ 问答频道 /正文

这里有个问题,我想打开一个文本文件,上面有一些ascii图片,然后在下面有一个关于程序的挥霍,问题是,当文件打开时,它已经准备好了向下滚动到about文本,所以ascii图片几乎看不见(除非我滚动回顶部)。有没有什么方法可以让它只打印第一部分,然后等待按键显示更多内容(类似于在linuxmandb中查看页面),或者类似的东西?在

到目前为止,我的代码是打开并显示文件:

    whereami = os.getcwd()
    importlogo = open(whereami+'/logo.txt', 'r')
    showlogo = importlogo.read()
    print showlogo

Tags: 文件方法文本程序内容ascii图片页面
1条回答
网友
1楼 · 发布于 2024-06-16 11:00:31

以下是我对你问题的看法:

import os
import sys


filename = 'logo.txt'
# Use join instead of hard-coding file separators
path = os.path.join(os.getcwd(), filename)
# You need to specify some kind of delimiter so you know
# where the logo ends. I couldn't think of a better way to
# only print part of the file.
delimiter = '   '


with open(path, 'r') as f:
    for line in f:
        if delimiter in line:
            sys.stdout.write('Press any key to continue...')
            raw_input()
        else:
            sys.stdout.write(line)

这会有助于了解徽标.txt文件看起来像。在

相关问题 更多 >