Skip to main content
 首页 » 编程设计

python之编写一个函数,反向返回一个data.txt

2025年05月04日11lori

<分区>

我是 Python 编程的新手,我一直被困在如何在函数中反向返回数据的问题上。

例如,如果 data.txt 包含:

Chaos reigns within.  
Reflect, repent, and reboot  
Order will return. 

调用 write_reversed('data.txt') 将打印:

Order will return. 
Reflect, repent, and reboot 
Chaos reigns within. 

我目前写的代码是

def write_reversed(filename): 
    """returns with words in reverse order in the file""" 
    with open(filename, 'r') as write_reversed: 
        for line in write_reversed: 
            return filename[::-1] 

我已将我的代码粘贴到 Pylint 检查器中,它显示“未使用的变量‘line’”