Skip to content

Instantly share code, notes, and snippets.

@aquinzi
Created July 12, 2013 18:07
Show Gist options
  • Save aquinzi/5986492 to your computer and use it in GitHub Desktop.
Save aquinzi/5986492 to your computer and use it in GitHub Desktop.
Script to merge txt files from a given folder to a file (utf-8)
# -*- coding: utf-8 -*-
"""
Script to merge txt files from a given folder
mergetxt path_directory path_output [options: --sep num --fname num]
path_directory: enter 'here' to take the path from where it runs
separator: separator between each file.
0 blank (default)
1 ****
2 ====
3 ++++
4 ####
5 ----
6 .*.*.*
7 <---->
fname: prints the filename, with or without path.
0 none (default)
1 name
2 with path
"""
import os,shutil,glob
import argparse
#Arguments
parser = argparse.ArgumentParser(description="Merge txt files from DIR in FILE",usage='%(prog)s path_directory path_output [options]',formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("file", help="path+output_file")
parser.add_argument("dir", help="path directory of files to merge. Type 'here' to take path from where script runs")
parser.add_argument("--sep", help='''Separator between each file.
0 blank (default)
1 ****
2 ====
3 ++++
4 ####
5 ----
6 .*.*.*
7 <---->
''',type=int, choices=[0, 1, 2, 3, 4, 5, 6, 7], default=0)
parser.add_argument("--fname", help='''prints the filename, with or without path.
0 none (default)
1 name
2 with path''',type=int, choices=[0, 1, 2], default=0)
args = parser.parse_args()
if args.file:
if ":\\" in args.file:
mergedFile = args.file
else:
mergedFile = os.getcwd() + '\\'+args.file
# si no especifica el .txt, se agrega
if not ".txt" in mergedFile: mergedFile = mergedFile + ".txt"
if args.dir == "here":
srcFiles = os.getcwd() #desde donde se corre
else:
if os.path.exists(args.dir):
srcFiles = args.dir
else: print ("Source DIR doesn't exist")
if args.sep == 0:
separator = "";
if args.sep == 1:
separator = "*************************************************************************"
if args.sep == 2:
separator = "=========================================================================="
if args.sep == 3:
separator = "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
if args.sep == 4:
separator = "#########################################################################"
if args.sep == 5:
separator = "-------------------------------------------------------------------------"
if args.sep == 6:
separator = ".*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*"
if args.sep == 7:
separator = "<------------------------------------------------------------------------>"
# Program per se
fn=""
with open(mergedFile,"wb")as output: #open for for write, replaces
for r,d,fi in os.walk(srcFiles):
for files in fi:
if files.endswith(u".txt"):
with open(os.path.join(r,files),'rb') as input:
if args.fname:
if args.fname==0: fn=""
if args.fname==1: fn=files+'\r\n'
if args.fname==2: fn=r+"\\"+files+'\r\n'
output.write(bytes(fn,'UTF-8'))
shutil.copyfileobj(input,output)
output.write(b'\r\n'+bytes(separator,'UTF-8')+b'\r\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment