I have a script here that looks for all the .txt files in a folder and then joins them into one file.
The problem is that some files have a "\ n" in the last line, causing the next line not to be below the previous one, causing errors when I import the final .txt.
It would be possible to check if the last line of a file has a "\ n" and thus delete it and if it does not, add a "\ n".
My files are in this format:
00000011098720150131379000100011
00000021098720150131379000400011
00000021098720150131379000400011
Here is the code:
import os
import glob
found = False
source_folder = None
while not found:
source_folder = str(input("Adicione o diretório com os arquivos.))
print(source_folder)
if not os.path.isdir(source_folder):
print(source_folder, 'A pasta não foi encontrada.)
else:
print("Pasta encontrada! ")
found = True
os.chdir(source_folder)
read_files = glob.glob("*.txt")
print(read_files)
arq = str(input("Adicione o nome do arquivo: "))
with open(arq, "wb") as outfile:
for f in read_files:
with open(f, "rb") as infile:
outfile.write(infile.read())