Skip to content

Instantly share code, notes, and snippets.

@mathigatti
Last active June 26, 2020 23:55
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 mathigatti/fb00495df3a8cf33f241adc592025e93 to your computer and use it in GitHub Desktop.
Save mathigatti/fb00495df3a8cf33f241adc592025e93 to your computer and use it in GitHub Desktop.
import os
import sys
def unir_txts(carpeta_con_los_txts):
lista_de_txts = os.listdir(carpeta_con_los_txts) # Esto arma la lista de todos los archivos que estan en ese directorio/carpeta
texto_final = "" # Variable donde voy a ir juntando el texto de todos los txts
for nombre_del_txt in lista_de_txts: # recorro los archivos uno por una
# uno el nombre de la carpeta con el nombre del archivo para armar la ruta exacta donde el txt se ubica
ruta_al_archivo = carpeta_con_los_txts + "/" + nombre_del_txt
# leo el archivo y agrego su texto a "texto_final"
with open(ruta_al_archivo,'r') as f:
texto_del_proximo_txt = f.read()
texto_final = texto_final + texto_del_proximo_txt.strip() + "\n"
# Escribo todo el texto en un archivo llamado "archivo_final.txt"
with open("todojunto.txt",'w') as f:
f.write(texto_final.strip())
if __name__ == '__main__':
carpeta_con_los_txts = sys.argv[1]
unir_txts(carpeta_con_los_txts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment