-5

Very reversed question here, I need to take a .txt file and add it to a list.

The output file:

abc123

What the list should be:

list = ["abc123"]
SecretLloyd
  • 109
  • 1
  • 13
  • 2
    Create an empty list. Open the file, read each line, and append it to the list. What is the difficulty? – John Gordon Jul 18 '21 at 01:02
  • 4
    The difficulty is I'm not that smart. – SecretLloyd Jul 18 '21 at 01:03
  • 2
    It's not about being smart. There's already a ton of tutorials and existing posts on how to read files and work with lists. As noted in [ask], doing your research should be your first step. – Gino Mempin Jul 18 '21 at 01:40

2 Answers2

3

You can use:

with open("","r") as f:
    data=f.readlines()
#Or same:
f=open("","r")
data=f.readlines()
f.close()

data is in list, it read each line. And put each line in list.

imxitiz
  • 3,920
  • 3
  • 9
  • 33
0

Do you mean add the content to a list? If so, you may use:

with open("filename") as f:
    content = f.read()
yourlist.append(content)
# Do something here...

If you want to add the file object to a list, you may use:

with open("filename") as f:
    yourlist.append(f)
    # Do something here...    
PRO
  • 169
  • 5