'TypeError: expected str, bytes or os.PathLike object, not int. In python, when I add some inputs and some function have to use it
Im new at coding and I'm doing a code for algorithms with a request matrix. I'm seeing this error when I add the code with the ***.
Context: I'm doing an input menu with options so the user can with number 1 create a matrix in excel and so python read it. With number 2 now I want to implement Dijkstra Algorithm.
I had search about this type of error and I know that happen because I'm putting a int and not a str, but I can't find the way to change it and so can read my dijkstra algorithm function requesting the initial node and the goal node.
Maybe I have some error in the parameters or variables. I'm not too good with functions calling. (Just starting to code)
import openpyxl as opxl
import sys
cant_nodos = 3
# FUNCIONES
## Crea la cantidad de nodos a utilizar y la restricción de un mínimo de nodos.
def min_nodos():
cant_nodos = int(input("Ingresar cantidad de nodos a utilizar (mínimo 6)"))
while(cant_nodos < 6):
print("ERROR: Elija mínimo 6 nodos y que sea entero positivo. ")
cant_nodos = int(input("Ingresar cantidad de nodos a utilizar (mínimo 6)"))
return cant_nodos
## Crea un menu
def crear_menu():
menu=int(input("Elija una opción \n 1.Crear parámetros \n 2.Aplicar Dijkstra \n 3.Aplicar Kruskal \n 4.Salir"))
if menu == 1:
min_nodos()
elif menu == 2:
*** empezar = str(input("Elija un nodo de inicio: "))
terminar = str(input("Elija un nodo de termino: ")) ***
dijkstra(cant_nodos, empezar, terminar)
elif menu == 3:
kruskal()
elif menu == 4:
sys.exit()
else:
print("\n ERROR: Elija una opción válida.")
crear_menu()
## Crea la matriz en excel
def crear_matriz_adyacente(cant_nodos):
libro = opxl.Workbook()
pagina = libro.active
pagina.title = "matriz_de_adyacencia"
lista_nodos = []
cont = 0
while(cont < cant_nodos):
contador = str(cont+1)
nodo = str(input("Ingrese nodo " +contador+ ":"))
if nodo not in lista_nodos:
lista_nodos.append(nodo)
pagina.cell(row = 1, column = cont+2, value = nodo)
pagina.cell(row = cont+2, column = 1, value = nodo)
cont = cont+1
else:
print("ERROR: Nodo existente, escoja otro: ")
for fila in range(len(lista_nodos)):
for columna in range(len(lista_nodos)):
if fila == columna:
valor = 0
elif columna > fila:
valor = int(input("Ingrese valor de nodo " +lista_nodos[fila]+" con el nodo " +lista_nodos[columna]+ ":"))
while(valor < 0):
print("ERROR: Valor negativo. Ingrese un valor positivo")
valor = int(input("Ingrese valor de nodo " +lista_nodos[fila]+" con el nodo " +lista_nodos[columna]+ ":"))
pagina.cell(row = fila+2, column = columna+2, value = valor)
pagina.cell(row = columna+2, column = fila+2, value = valor)
libro.save("matriz_adyacente.xlsx")
return crear_menu()
## Abre la matriz para utilizarla
def abrir_matriz_adyacente(matriz_adyacente):
excel = opxl.load_workbook(matriz_adyacente)
lista_excel = []
pagina = excel.active
maximo_columna = pagina.max_column
maximo_fila = pagina.max_row
for fila in range(1, maximo_fila+1):
lista_fila = []
for columna in range(1, maximo_columna+1):
espacio = pagina.cell(row = fila, column = columna)
lista_fila.append(espacio.value)
lista_excel.append(lista_fila)
return lista_excel
def dijkstra(cant_nodos, empezar, terminar):
lista_excel = abrir_matriz_adyacente(cant_nodos)
camino_mas_corto = {}
pista_predecedor = {}
nodos_no_explorados = lista_excel
inf = 9999999
pista_camino = []
for nodo in nodos_no_explorados:
camino_mas_corto[nodo] = inf
camino_mas_corto[empezar] = 0
while nodos_no_explorados:
nodo_min_distancia = None
for nodo in nodos_no_explorados:
if nodo_min_distancia is None:
nodo_min_distancia = nodo
elif camino_mas_corto[nodo] < camino_mas_corto[nodo_min_distancia]:
nodo_min_distancia = nodo
opciones_caminos = lista_excel[nodo_min_distancia].items()
for nodo_hijo, peso in opciones_caminos:
if peso + camino_mas_corto[nodo_min_distancia] < camino_mas_corto[nodo_hijo]:
camino_mas_corto[nodo_hijo] = peso + camino_mas_corto[nodo_min_distancia]
pista_predecedor[nodo_hijo] = nodo_min_distancia
nodos_no_explorados.pop(nodo_min_distancia)
nodo_actual = terminar
while nodo_actual != empezar:
try:
pista_camino.insert(0, nodo_actual)
nodo_actual = pista_predecedor[nodo_actual]
except KeyError:
print("El camino no existe")
break
pista_camino(0, empezar)
if camino_mas_corto[terminar] != inf:
print("Distancia más corta es: " + str(camino_mas_corto[terminar]))
print("Camino optimo es: " + str(pista_camino))
crear_menu()
crear_matriz_adyacente(cant_nodos)
lista_excel = abrir_matriz_adyacente("matriz_adyacente.xlsx")
print(lista_excel)
Thank you!
Solution 1:[1]
The issue is
lista_excel = abrir_matriz_adyacente(cant_nodos) # cant_nodos = 3
which calls
excel = opxl.load_workbook(matriz_adyacente) # matriz_adyacente = cant_nodos = 3
opxl.load_workbook expects string/bytes as an argument, not an int. Your program isn't that straightforward to understand, so I'm not sure why you're passing an int to the function. Just replace it with the filename of the sheet that you want to open.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Abhinav Mathur |
