Skip to content

Instantly share code, notes, and snippets.

@cloudchou
Last active November 15, 2016 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloudchou/60f97929bcd52ce779a4fe3bb166d54a to your computer and use it in GitHub Desktop.
Save cloudchou/60f97929bcd52ce779a4fe3bb166d54a to your computer and use it in GitHub Desktop.
合并两个文件内容的python脚本
# -*- coding: utf8 -*-
#!/usr/bin/python
import os
import re
import codecs
import sys
import tempfile
import string
reload(sys)
sys.setdefaultencoding('utf-8') # 允许打印unicode字符
content_dir = 'C:\Users\Administrator\Desktop\docs'
meta_dir = "E:\\git\\cloudchou.github.io\\_posts"
def get_file_content(filename):
f = codecs.open(filename, 'r', 'utf-8')
s = f.readlines()
f.close()
return s
def write_file_content(filename, content, append):
if append:
f = codecs.open(filename, 'a+', 'utf-8')
else:
f = codecs.open(filename, 'w', 'utf-8')
f.writelines(content)
f.close()
files = os.listdir(meta_dir)
for filename in files:
filename = unicode(filename, 'gbk')
meta_file_path = unicode(meta_dir, 'gbk') + "\\" + filename
meta_file_content = get_file_content(meta_file_path)
meta_file_content = re.sub(
r'(---.*---).*', r'\1', string.join(meta_file_content,''), flags=re.M | re.S)
tmpfd, tempfilename = tempfile.mkstemp()
write_file_content(tempfilename, meta_file_content+"\r\n", False)
content_file_name = re.sub(
r'(\d{4}-\d{2}-\d{2})-(.*)\.md', r'\2'+'.md', filename)
print content_file_name
content_file_path = unicode(content_dir, 'gbk') + "\\" + content_file_name
if not os.path.exists(content_file_path):
print content_file_name+" can not be done"
continue
content_file_content = get_file_content(content_file_path)
content_file_content = string.replace(
string.join(content_file_content,''), "\n", "\r\n")
write_file_content(tempfilename, content_file_content, True)
# os.remove(meta_file_path)
# os.rename(tempfilename, "D:\1.txt")
tmp_file_content = get_file_content(tempfilename)
write_file_content(meta_file_path, tmp_file_content, False)
# newFileName, _ = re.subn(
# r'(\d{4}-\d{2}-\d{2})-.*\.md', r'\1-'+'.md', filename)
# print newFileName
# break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment