#input
inPut = input("Please enter a file name: ")
outPut = input("Please enter a file name which you would like the file reversed: ")
#open
infile = open(inPut, "r")
outfile = open(outPut, "w")
#list main file
line = infile.readline()
line = line.rstrip()
while line != "" :
print(line)
line = infile.readline()
line = line.rstrip()
#output file in reversed
outfile.write(
####confused here######
#close files
infile.close()
outfile.close()
所以我一直试图弄清楚这一点,我在下面的书中找到了这段代码,假设它是向后列出一个文件。我很困惑如何将其应用于我的代码。我的主要目标是编写一个程序,读取文件中的每一行,然后反转它的行并将它们写入另一个文件。
for line in reversed(list(open("filename"))):
print(line.rstrip())