Search code examples
pythonpython-3.xfilevariablesline

Assigning `.txt` file contents to a list in python


I am making an app that stores its settings in a .txt file.
I am able to get the line count, but I don't know, how to store the different lines as values in a list.

For example:

linecount = 0
datainfile = [] 

with open("txt.txt" , "r") as t:
    linecount += 1

config1 = datainfile[0] 

I have tried looking around on the internet, but could not find anything.


Solution

  • This is how I did it:

    lines = [] 
    with open("txt.txt", "r") as t:
        for i in t:
            lines.append(t)
    config1 = datainfile[0]