OSError: [Errno 22] Invalid argument: './C:\\Python\\\x07file.txt'

0

I am doing a small program in Python and it throws this error in the second line of code

  

OSError: [Errno 22] Invalid argument: './C:\Python\\x07file.txt'

def funcion():
    ruta = open('./%s' % 'C:\Python\\archivo.txt', 'r')
    archivo_leido = ruta.read()
    print (archivo_leido)
    ruta.close()

funcion()
    
asked by Miguel Cabrera 06.05.2017 в 02:44
source

1 answer

0

Try to use the keyword with to avoid closing the file manually and freeing it automatically.

def funcion():
    with open("C:\Python\archivo.txt","r") as f:
        archivo_leido = f.read()
        print(archivo_leido)


funcion()

Also, if you need to format your chain with different parameters, I advise you to use the format function on %

def funcion():
    with open("{0}".format("C:\Python\archivo.txt"),"r") as f:
        archivo_leido = f.read()
        print(archivo_leido)


funcion()
    
answered by 06.05.2017 в 21:27