# Build #PC-173.4127.16, built on December 19, 2017
# JRE: 1.8.0_152-release-1024-b8 amd64
# JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
# Windows 10 10.0
# Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36)
# [MSC v.1900 64 bit (AMD64)] on win32
if __name__== "__main__":
filename = input("Please input the name of file:")
f = open(filename,"w") # 以写的形式打开一个文件
while 1: # 1 的效率是最高的
context = input("Please input context('EOF' will close file): ")
if context == "EOF":
f.close()
break
else:
f.write(context)
f.write("\n")
# Build #PC-173.4127.16, built on December 19, 2017
# JRE: 1.8.0_152-release-1024-b8 amd64
# JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
# Windows 10 10.0
# Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36)
# [MSC v.1900 64 bit (AMD64)] on win32
import codecs
file = "passwd"
sortfile = "sortpasswd.txt"
filecontext = []
sortuid = []
with codecs.open(sortfile,"wb") as fsort:
with codecs.open(file,encoding="utf-8") as f:
filecontext += f.readlines()
for line in filecontext:
sortuid.append(int(line.split(":")[2]))
sortuid.sort()
for uid in sortuid:
for line in filecontext:
if str(uid) == line.split(":")[2]:
print(line)
fsort.write(line.encode("utf-8"))