python 两文件合并

有两个磁盘文件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()

python3 方案:

# -*- coding: UTF-8 -*- 
with open('test1.txt') as f1, open('test2.txt') as f2, open('2.txt', 'w') as f3:
for a in f1: 
    b = f2.read() 
c = list(a + b) c.sort() 
d = '' 
d = d.join(c) 
f3.write(d)

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值