txtmerge.py 812 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env python
  2. # usage: txtmerge.py <input_dir> <output_file_name>
  3. # does the exact opposite of txtsplit.py
  4. import os
  5. import sys
  6. import re
  7. import io
  8. assert len(sys.argv) == 3, "usage:\ntxtmerge.py <input_dir> <output_file_name>"
  9. inputdir = str(sys.argv[1])
  10. outputfilename = str(sys.argv[2])
  11. with io.open(outputfilename, 'w', encoding='utf-16', newline='\r\n') as outputfile:
  12. #for file in os.listdir(inputdir):
  13. for root, subdirs, files in os.walk(inputdir):
  14. for filename in files:
  15. filepath = os.path.join(root,filename)
  16. with io.open(filepath, 'rt', encoding='utf-8') as inputfile:
  17. text = inputfile.read()
  18. # EOF
  19. if text[-1] != u'\n':
  20. text += u'\n\n'
  21. outputfile.write(text)