如何在Python 3中与txt文件分别打印行?

时间:2018-09-27 10:57:44

标签: python python-3.x

我正在编写一个脚本,该脚本需要从txt文件打印第1行,休息3秒钟,打印第2行,休息3秒钟,然后继续直到以这种方式打印整个文本文件。我该如何实现?

1 个答案:

答案 0 :(得分:0)

import time
file = open("abc.txt","r") #<-- Opening file as read mode
data = file.read()         #<-- Reading data
file.close()               #<-- Closing file
data = data.split("\n")    #<-- Splitting by new lines
for i in data:             #<-- Looping through splitted data
    print(i)               #<-- Printing line
    time.sleep(3)          #<-- Waiting for 3 seconds