Skip to content

Instantly share code, notes, and snippets.

@marcoacierno
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save marcoacierno/d2fd23dad63c73cabb40 to your computer and use it in GitHub Desktop.

Select an option

Save marcoacierno/d2fd23dad63c73cabb40 to your computer and use it in GitHub Desktop.
Python - Read all files and merge them in one file -- First argument: folder start. Second arguments: folders to ignore (use ,)
from genericpath import exists
import os
from os.path import relpath, dirname
__author__ = 'Marco A.'
import sys
if __name__ == "__main__":
arguments_length = len(sys.argv) - 1
if arguments_length < 1:
print("Passa l'indirizzo della directory")
sys.exit()
source_dir = sys.argv[1]
if arguments_length > 1:
folders_to_ignore = sys.argv[2].split(",")
else:
folders_to_ignore = []
if not exists(source_dir):
print("Source dir non valida")
sys.exit()
parent_path = dirname(source_dir)
with open("output.txt", "w") as output_file:
output_file.truncate()
for root, dirs, files in os.walk(source_dir):
dirs[:] = [d for d in dirs if not root + "\\" + d in folders_to_ignore]
for file_name in files:
file_relative_path = relpath(root, parent_path)
output_file.write("## " + file_relative_path + "\\" + file_name + " ##")
output_file.write("\n")
output_file.write("\n")
with open(root + "\\" + file_name) as content:
output_file.write(content.read())
output_file.write("\n")
output_file.write("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment