python 将多个文件写入到另一个文件里面

#coding=utf-8 
import time
import datetime
import os
#获取目标文件夹的路径
filedir = os.getcwd()+'/sss'
#获取当前文件夹中的文件名称列表  
filenames=os.listdir(filedir)
#打开当前目录下的result.txt文件,如果没有则创建
f=open('文件的路径','w+')
#先遍历文件名
for filename in filenames:
        #完整的路径
        filepath = filedir+'/'+filename
        s = os.path.splitext(filename)[1]
        #判断是否是文件
        if s == '.log' and os.path.isfile(filepath):
                #遍历单个文件,读取行数
                for line in open(filepath):
                        #for line in line.readlines():
                                ls = line.strip().split("|")
                        #       w = datetime.datetime.strptime(ls[3],'%Y-%m-%d %H:%M:%S') ##打印循环都是 时间  2020-02-02 12:21:45
                                u = int(time.mktime(datetime.datetime.strptime(ls[3],'%Y-%m-%d %H:%M:%S').timetuple()))
                                j = ls[0]+'|'+ls[1]+'|'+ls[2]+'|'+ls[3]+'|'+ls[4]+'|'+ls[5]+'|'+ls[6]+'|'+ls[7]+'|'+ ls[8]+'|'+ls[9]+'|'+ls[10]+'|'+ls[11]+'|'+ls[12]+'|'+ls[13]+'|'+ str(u)+'\n'
                        #       print(j)
                                #b.writelines(j+'\n')
                                f.write(j)
                #for line in open(filepath):
                #       print(line)
                #       f.writelines(line+'\n')
                        #f.write(line)
#关闭文件
f.close()

另一种


import os
 
def saveFile(filename, content):
    f = open(filename, "w", encoding="utf-8")
    f.write(content)
   
def combinetxt(rootdir,filename):
    f = open(rootdir+filename, "w",encoding="utf-8")#创建文件
    list = os.listdir(rootdir) #枚举文件夹下所有文件
    for i in range(0,len(list)):#遍历
            path = os.path.join(rootdir,list[i])#完整路径
            if os.path.isfile(path):#如果是文件
                content = readFile(path)#读取所有
                f.write("*****************************")
                f.write(content)#写入
    f.close();#关闭
 
 
def start():
    combinetxt("E:/aa","ca.txt")
    combinetxt("E:/bb","cb.txt")
 
 
start()
Python读取两个txt文件内容,重新写到新的txt文件
https://blog.csdn.net/iprotn/article/details/88576357
#python读写excel(附上将一个文件夹下所有excel文件合并成一个文件的代码)
https://www.jianshu.com/p/661ba285641c
#Python多线程读写文件加锁
https://blog.csdn.net/qq_30554229/article/details/80094253
#python将多个txt文件读取,并写成一个txt
https://blog.csdn.net/qq_38640439/article/details/81410380
#python学习笔记--将文件若干行写入另一个文件
https://blog.csdn.net/qq_28808697/article/details/80054668
#Python实现将一个文件夹下面的多个txt文件合并成一个txt文件
https://blog.csdn.net/weixin_40490880/article/details/84066842
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟伟哦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值