Python - Does not work from for

Asked

Viewed 27 times

0

import os, sys, string

# coding: utf-8
from typing import TextIO

lista: TextIO = open("C:\\Users\\Eunice\\Desktop\\contas.txt", "r", encoding="utf-8")

print(lista.readlines())
linha = lista.readlines()

for linha in lista:
    arquivo = open("C:\\Users\\Eunice\\Desktop\\siga\\seguindo\\user_-_data\\database\\login.ini", "w")    
    arquivo.write("[headers]" "\ntitle = Config File \n" "\ncompression = yes\n""\ncompression_level = 9\n" "\n[configuracoes]\n""\nlineedit_login = email\n""\nlineedit_senha = senha\n""\ntexteditcontas =", linha, "\n senha\n")
    arquivo = open(arquivo, "r")
    print(arquivo)
    arquivo.close()
os.startfile(r"C:\Users\Eunice\Desktop\siga\siga2\seguindo.exe")`
    

I have this code, until the lista.readlines works, displays the list, but then only executes the os.startfile, I don’t know what to do

  • lista: TextIO = open(.... shouldn’t be lista = open(.....

  • Basically, it is the same problem of the question indicated in the blue box above: when you call readlines, it reads all the contents of the file, so when to call again or do the for, there’s nothing left to read.

  • Exchange your for linha in lista: for while linha: that will work

  • 1

    @Evilmaax while linha doesn’t solve (see) - the problem is he used readlines, which reads all the contents of the file, and then when it arrives at the for, there’s nothing left to read. So either he just calls readlines once (and in for it uses the returned list), or it does not use readlines and does the for directly.

  • While you’re at it, consider using with to open the files, because it ensures that they will be closed at the end. And how you open the output file every time inside the for, and with the option w, it will be overwritten at each iteration. If this is not the intention, open only once outside the for

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.