Read files with python, problem getting value with .readline ()


I have a problem when I want to execute as parameters or variables the data I extract from a txt file. For example:

conexion = kinterbasdb.connect(dsn=str(dir),user=str(user), password=str(password))
 global variable1
 variable1 = 'C:/carpeta/etc/'

Y dir is a variable where I store the data extracted from a txt:

    dir_tb= variable1
    archivo = open('texto.txt','w') 

    archivo = open('texto.txt','r') 
    dir = archivo.readline()

But if I replace the dir parameter with the global variable it works fine.

asked by Nahuel Jakobson 31.03.2016 в 00:46

2 answers


You comment that when you use:

variable1 = 'C:/carpeta/etc/'

as a value to define dsn=str(dir) and it works, but if you read the value of the file does not work:

dir = archivo.readline()

It should work without any problem, I suggest you check what value you have in the line that you get from the file

print 'valor de dir : ',  str(file.readline())

I could assure you that the problem is that the line you are reading from the file, does not actually contain the value 'C:/carpeta/etc/'

answered by 31.03.2016 / 02:59

Thanks for your answer @ChemaCortes @Elenasys, he was in the reading line. The solution was to eliminate spaces with line[0].strip()

answered by 01.04.2016 в 14:19