-1

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)
John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • You haven't said what the actual problem is. When your code prints `print("Excel file was generated:", ruta_archivo_excel)`, does that file exist, and does it contain the expected contents? – John Gordon Apr 17 '23 at 01:06
  • Unless you are specifically asking about how to solve a cross-version compatibility problem (in which case your question should obviously describe that problem) you should not mix the [tag:python-2.7] and [tag:python-3.x] tags. – tripleee Apr 17 '23 at 06:05

1 Answers1

0

Yes it is not being generated because you are using a local address in your code. Pycharm may not have permissions to write to that directory.

To fix this, you can try using an absolute path instead of a relative path

try using os.path.abspath(ruta_carpeta) to get the absolute path of the directory

New

import pandas as pd
import os

ruta_carpeta = r"C:\Users\José\Documents\prueba1_validador"

# Get the absolute path of the directory
ruta_carpeta = os.path.abspath(ruta_carpeta)

archivos_txt = [archivo for archivo in os.listdir(ruta_carpeta) if archivo.endswith(".txt")]

filename_to_column = {
    "24645": "1",
    "33033": "2",
    "57375": "3",
    "38831": "4",
    "79441": "5",
    "50025": "6"
}

df = pd.DataFrame()

for archivo in archivos_txt:
    ruta_archivo = os.path.join(ruta_carpeta, archivo)

    # Read the file as a DataFrame
    df_archivo = pd.read_csv(ruta_archivo, sep="\s+", header=None, skiprows=11)

    # Add the column based on the filename
    column = filename_to_column.get(archivo[:5], "")
    df_archivo["Columna"] = column

    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)

Deadlooks
  • 132
  • 10