Reverse the order of lines in a text file while preserving the contents of each line: Difference between revisions
Reverse the order of lines in a text file while preserving the contents of each line (view source)
Revision as of 19:30, 4 October 2021
, 2 years agoAdded Python implementation
(Ada version) |
(Added Python implementation) |
||
Line 316:
</pre>
=={{header|Python}}==
Interactive program which takes input from a file :
<lang python>
#Aamrun, 4th October 2021
import sys
if len(sys.argv)!=2:
print("Usage : python " + sys.argv[0] + " <filename>")
exit()
dataFile = open(sys.argv[1],"r")
fileData = dataFile.read().split('\n')
dataFile.close()
[print(i) for i in fileData[::-1]]
</lang>
Input file :
<pre>
"Diplomacy is the art of
saying 'Nice Doggy'
until you can find a rock."
--- Will Rodgers
</pre>
Sample run and output:
{{out}}
<pre>
C:\My Projects\BGI>python rosetta7.py diplomaticQuote.txt
--- Will Rodgers
until you can find a rock."
saying 'Nice Doggy'
"Diplomacy is the art of
C:\My Projects\BGI>
</pre>
=={{header|R}}==
<lang R>text <- scan("Rodgers.txt", character(), sep = "\n")
|