Python Forum
How to import list from file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to import list from file?
#1
Hey, i've got a list inside a file like ["one", "two", "three"], which i'd like to import into my python script. I've tried it with
list1 = open("list1.txt", "r") but that didn't work. Could somebody help me with this please? I'm new to python and i just need to fix this one last problem.

This is the whole script.

import random, time, tweepy

list1 = open("list1.txt", "r")
list2 = open("list2.txt", "r")
list3 = open("list3.txt", "r")

def one():
    return random.choice(list1) + random.choice(list2)

def two():
    return random.choice(list2) + random.choice(list3)

#I'm working on a Twitter bot, that's just some twitter stuff down here 
consumer_key = 'XXX'
consumer_secret = 'XXX'
access_token = 'XXX'
access_token_secret = 'XXX'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

while True:
    postthis = random.choice([one,two])()
    if len(postthis) <= 140:
        api.update_status(status=postthis)
        time.sleep(10)
And the error message:

Traceback (most recent call last):
  File "C:/Users/User/Desktop/Example/example.py", line 22, in <module>
    postthis = random.choice([one,two])()
  File "C:/Users/User/Desktop/Example/example.py", line 11, in two
    return random.choice(list2) + random.choice(list3)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\random.py", line 256, in choice
    i = self._randbelow(len(seq))
TypeError: object of type '_io.TextIOWrapper' has no len()
>>>
I also don't understand the error has no len(). The content of my files are just very long lists, nothing else included. Maybe somebody has an alternative solution? Thanks so much :)
Reply
#2
You can use
import ast
with open('list1.txt') as infile:
    list1 = ast.literal_eval(infile.read())
The way you wrote things, list1 is an opened file object, not a list.

(Apr-05-2018, 04:47 PM)Epileptiker Wrote: I also don't understand the error has no len().
The call to random.choice() evaluates the length of its argument. Python informs you that an open file object has no method to compute its length.
Reply
#3
how do i can share my query to the forum
Reply
#4
Oh my god it works. Awesome, thank you so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Video doing data treatment on a file import-parsing a variable EmBeck87 15 3,009 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Import XML file directly into Excel spreadsheet demdej 0 882 Jan-24-2023, 02:48 PM
Last Post: demdej
  How from sklearn.datasets import load_diabetes change to import to CSV file Anldra12 0 1,881 Dec-25-2021, 07:20 PM
Last Post: Anldra12
  How to import file and function in another folder SriRajesh 1 3,224 Dec-18-2021, 08:35 AM
Last Post: Gribouillis
  Python - Import file sequence into Media Pool jensenni 1 2,168 Feb-02-2021, 05:11 PM
Last Post: buran
Smile Import error with local file colt 1 1,972 Nov-08-2020, 07:56 AM
Last Post: Gribouillis
  Code import .CSV file to MySQL table rtakle 4 2,919 Apr-30-2020, 03:16 PM
Last Post: anbu23
  Cannot import list from another module? t4keheart 1 2,296 Feb-17-2020, 10:25 PM
Last Post: t4keheart
  Regarding import library in two different program file Rohit 3 2,513 Jan-22-2020, 07:14 AM
Last Post: buran
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 2,643 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020