I am using this code to open several TXTs with certain numbers in their name and it starts to take the data from row 12 and below. When it finds them, it adds a column to the right with an identifying number. And the joined file of the 6 TXT files are saved in an excel file called join. The problem is that I get the message "Process finished with exit code 0" but I don't get the file generated in Pycharm. Is it because I use a local address where the folder with my files is?
import pandas as pd
import os
import re
ruta_carpeta = r"C:\Users\José\Documents\prueba1_validador"
archivos_txt = [archivo for archivo in os.listdir(ruta_carpeta) if archivo.endswith(".txt")]
df = pd.DataFrame()
for archivo in archivos_txt:
ruta_archivo = os.path.join(ruta_carpeta, archivo)
with open(ruta_archivo, "r") as f:
contenido_archivo = f.readlines()[11:]
df_archivo = pd.DataFrame([linea.strip().split() for linea in contenido_archivo])
if re.search(r"24645", archivo):
df_archivo["Columna"] = "1"
elif re.search(r"33033", archivo):
df_archivo["Columna"] = "2"
elif re.search(r"57375", archivo):
df_archivo["Columna"] = "3"
elif re.search(r"38831", archivo):
df_archivo["Columna"] = "4"
elif re.search(r"79441", archivo):
df_archivo["Columna"] = "5"
elif re.search(r"50025", archivo):
df_archivo["Columna"] = "6"
else:
df_archivo["Columna"] = ""
df = pd.concat([df, df_archivo], ignore_index=True)
ruta_archivo_excel = os.path.join(ruta_carpeta, "join.xlsx")
with pd.ExcelWriter(ruta_archivo_excel) as writer:
df.to_excel(writer, index=False)
print("Excel file was generated:", ruta_archivo_excel)
Tried to use this simple code as an example to test but couldn't work out.
ruta_archivo = r"C:\Users\José\Documents\prueba1_validador\TXT\33033_TRA2.txt"
with open(ruta_archivo, 'r') as f:
contenido = f.read()
print(contenido)