open a file in Python, asking the name first

1

Dear, I have a problem, it turns out that I have a code and I want the user to give me the name of the file (including the .txt) and that this can open the file, the point is that to open it you need the quotes , but as this name I keep it inside a variable, if I put the name of the variable in quotes it happens it becomes a string. I leave the code, thank you very much

nombre=input("ingrese nombre del archivo (con el .txt por favor): ")
archivo=open(nombre)
lineas=arch.readlines()
print(lineas)
for linea in lineas:
    numer=linea.split()
    print(numer)
    qw.append(numer)
for i in range(0,len(qw)):
    if qw[i][0]=="G":
        posicion_gallina(int(qw[i][1]),int(qw[i][2]),int(qw[i][3]),int(qw[i][4]))
        jug=2
    else:
        posicion_coyote(int(qw[i][1]),int(qw[i][2]),int(qw[i][3]),int(qw[i][4]))
        jug=1
arch.close()
    
asked by VICENTE DANIEL PALACIOS 19.10.2017 в 03:37
source

1 answer

1

Good morning,

I have changed the name of the variable arch, by file and it has read perfectly:

    
answered by 19.10.2017 в 12:10