我们将以相反的顺序显示文本文件的内容。为此,我们首先创建一个包含以下内容的文本文件 amit.txt
现在让我们以相反的顺序读取上述文件的内容 -
# The file to be read with open("amit.txt", "r") as myfile: my_data = myfile.read() # Reversing the data by passing -1 for [start: end: step] rev_data = my_data[::-1] # Displaying the reversed data print("Reversed data = ",rev_data)
Reversed data = !tisisihT
# Opening the file to read my_data = open('amit.txt','r') # reversing the data for myLine in my_data: l = len(myLine) rev_data = '' while(l>=1): rev_data = rev_data + myLine[l-1] l=l-1 print("Reversed data = ",rev_data) # Displaying the reversed data
Reversed data = !tisisihT
以上是Python - 以相反的顺序显示文本文件的内容的详细内容。更多信息请关注PHP中文网其他相关文章!