1.      把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件内容,然后反序,在追加到文件的下一行中

import codecs

lst = ['a', '34', 't', 'x', '5', '11']

with codecs.open('test.txt', 'wb') as f:

    f.writelines(sorted(lst))

with codecs.open('test.txt', 'rb') as g:

    l = g.read()

    string = '\n'+ l[::-1]

with codecs.open('test.txt', 'ab') as h:

    h.write(string)


2.      分别把 string, list, tuple, dict写入到文件中

import codecs

import json

lst = ['a', '34', 't', 'x', '5', '11']

str = 'abcdefghijklmn'

tup = ('uuuu','vvvv')

dic = {'k':123, 'l':456}

with codecs.open('test2.txt', 'wb') as f:

    f.writelines(lst)

    f.write('\n'+str+'\n')

    f.writelines(tup)

    f.write('\n')

    js = json.dumps(dic)

    f.write(js)