Skip to content

Instantly share code, notes, and snippets.

@good-old-times
Created September 13, 2018 11:08
Show Gist options
  • Save good-old-times/2029608529ae64bb67603d49465744c2 to your computer and use it in GitHub Desktop.
Save good-old-times/2029608529ae64bb67603d49465744c2 to your computer and use it in GitHub Desktop.
The teacher asks me for this code. The sorting of words in somefile using array, for, sorted(), open()
import sys
data = []
datalen = 0
with open(sys.argv[1],'r') as f:
for line in f:
for word in line.split():
datalen=datalen+1
data.insert(datalen, " "+word)
print("Array data: (NO SORTING)")
print(data)
data=sorted(data)
print("SORTED:")
print(data)
open(sys.argv[1],'r+').writelines(data)
# for execute for example: python __main__.py data.txt
# data.txt: 1 2 3 4 4 5 6 7 8 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment