Add header and footer to some file

今天整理资料的时候,发现要在很多文件中的头部和尾部添加相同的文本,于是自己使用Python做了一个简单的文件拼接功能,也可以说是文件追加功能,给一个文件批量追加头尾内容,达到省事的效果,顺便还可以练习下Python。下面来介绍下:

现在有三个文件,如下:

  • content.txt 位于一个叫path的文件中;
  • header.txt用于添加到content.txt头部的文件;
  • footer.txt用于添加到content.txt尾部的文件。

现在要实现的功能就是,将header和footer分别添加到content的头部和尾部。


函数说明:

  • add_footer(infile, outfile):用于将footer内容添加到content中,第一个参数表示的添加到尾部的文件,如输入footer.txt,第二个为内容文件。如content.txt文件
  • add_header(infile, outfile, auto=True): 用于将一个文件放入好另一个文件的头部,如果auto=Ture,则不对内容做修改,auto为False的话,这里添加了部分需要的东西,如文件的创建时间、标题等信息。
  • addHeadAndFooter(path, header, footer, auto=False):核心函数,调用头尾两个方法,此处的path为文件夹名称,该函数的功能是将path文件夹下的所有文件都添加头和尾的内容,auto默认为False,功能和上面的相同。
  • getStdTime(seconds):将时间戳格式的日期转换为标准格式,如:2015-11-03 10:24

代码(AddHeader.py):

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 03 10:32:26 2015

@author: liudiwei
"""

import os,time

def add_footer(infile, outfile):
    with open(infile,'r') as inputfile:
        with open(outfile,'a') as outfile:
            outfile.write("\n\n"+''.join(inputfile.readlines()))

#如果auto==True,直接将文件内容加入到当前文件
def add_header(infile, outfile, auto=True): 
    inf=open(infile,'r')
    outf = open(outfile,'r')
    header = inf.readlines()
    content=outf.readlines()
    if auto==True:
        with open(outfile,'w') as output:
            output.write(''.join(header)+ "\n\n" \
                            +''.join(content))  
    else:
        ctime=getStdTime(os.path.getctime(outfile))
        title="title: " + outfile.split('/')[1].split('.')[0]
        print title
        add_content="---\n"
        add_content=add_content+title+'\n'  #add title
        add_content=add_content+ctime +'\n' #add date
        add_content=add_content+''.join(header)
        with open(outfile,'w') as output:
            output.write(''.join(add_content)+ "\n\n" \
                        +''.join(content))  
    outf.close()
    inf.close()




def addHeadAndFooter(path, header, footer, auto=False):
    filelist=os.listdir(path)
    for eachfile in filelist:
        add_header(header,path + "/" + eachfile, auto)
        add_footer(footer,path + "/" + eachfile)   


def getStdTime(seconds):
    x = time.localtime(seconds)
    return "date: "+ time.strftime('%Y-%m-%d %H:%M:%S',x)


if __name__=='__main__':
    if (len(os.sys.argv)<4):
        raise TypeError()
    else:
        print "os.sys.arg"
    #path="path"
    #header="head.md"
    #footer="footer.md"
    os.chdir(".")
    path=os.sys.argv[1]
    print path
    header=os.sys.argv[2]
    footer=os.sys.argv[3]
    filelist=os.listdir(path)
    addHeadAndFooter(path,header,footer)
    print "Success added!"

#----------------    
# command 
# python AddHead.py "path" "header.txt" "footer.txt"
#----------------

直接在console控制台上运行下列代码即可

python AddHeader.py "path" "header.txt" "footer.txt"


此文乃博主即兴之作,如果你从中有所收获,欢迎前来赞助,为博主送上你的支持:【赞助中心】
个人博客: 【D.W BLOG】
新浪微博: 【@拾毅者】

你可以在 RecyclerView 中添加头部和尾部视图来实现添加 headerfooter 的效果。下面是一种常见的实现方式: 首先,你需要创建两个布局文件用作 headerfooter 的视图。例如,header_view.xml 和 footer_view.xml。 然后,在你的 RecyclerView 的适配器中,你需要创建两个常量来表示 headerfooter 的视图类型。例如,HEADER_VIEW_TYPE 和 FOOTER_VIEW_TYPE。 接下来,在适配器中,你需要重写以下几个方法: 1. getItemViewType(int position) 方法:根据 position 来返回相应的视图类型。如果 position 是 0,则返回 HEADER_VIEW_TYPE;如果 position 是数据集合的大小加上 1,则返回 FOOTER_VIEW_TYPE;否则返回普通的 item 类型。 2. onCreateViewHolder(ViewGroup parent, int viewType) 方法:根据 viewType 来创建对应的 ViewHolder。如果 viewType 是 HEADER_VIEW_TYPE 或 FOOTER_VIEW_TYPE,则使用相应的布局文件创建 ViewHolder;否则使用普通的 item 布局文件创建 ViewHolder。 3. onBindViewHolder(ViewHolder holder, int position) 方法:根据 position 来绑定数据到 ViewHolder。如果 position 是 HEADER_VIEW_TYPE 或 FOOTER_VIEW_TYPE,则不需要绑定数据;否则绑定普通的 item 数据。 最后,在你的 RecyclerView 中设置适配器,并在数据集合中添加对应的数据项作为 headerfooter。例如,使用以下代码: ``` MyAdapter adapter = new MyAdapter(dataList); adapter.addHeader(headerData); adapter.addFooter(footerData); recyclerView.setAdapter(adapter); ``` 请注意,上述代码中的 MyAdapter 是你自定义的 RecyclerView.Adapter 子类,其中包含了添加 headerfooter 的方法。 以上就是在 RecyclerView 中添加 headerfooter 的基本步骤。希望能对你有所帮助!如有需要,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值