0
i have a script here that searches all the files . txt from a folder and then joins them in one file only.
The problem is that some files have a " n" in the last row, causing the next line not to be below the previous one, causing errors when I import. final txt.
It would be possible to check to see if the last line of a file has an " n" and so delete it and if not, add a " n".
My records are in that 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())
Wow! The code got very clean this way. Thank you so much for your help @Lacobus.
– Guilherme Oliveira