Python 练习实例99

4050 1
LZH实名认证 2022-3-17 19:30:06 | 显示全部楼层 |阅读模式
题目:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中。

程序分析:无。

程序源代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

if __name__ == '__main__':
    import string
    fp = open('test1.txt')
    a = fp.read()
    fp.close()

    fp = open('test2.txt')
    b = fp.read()
    fp.close()

    fp = open('test3.txt','w')
    l = list(a + b)
    l.sort()
    s = ''
    s = s.join(l)
    fp.write(s)
    fp.close()
运行以上程序前,你需要在脚本执行的目录下创建 test1.txt、test2.txt 文件。

以上程序执行成功后,打开 test3.txt 文件可以看到内容如下所示:

123456
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行