1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| # -*- 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" #----------------
|