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
  How to write variable in a python file then import it in another python file? tatahuft 4 859 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  import a function from another file using relative path paul18fr 6 2,680 Aug-01-2024, 06:40 AM
Last Post: paul18fr
Video doing data treatment on a file import-parsing a variable EmBeck87 15 5,480 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Import XML file directly into Excel spreadsheet demdej 0 1,557 Jan-24-2023, 02:48 PM
Last Post: demdej
  How from sklearn.datasets import load_diabetes change to import to CSV file Anldra12 0 2,656 Dec-25-2021, 07:20 PM
Last Post: Anldra12
  How to import file and function in another folder SriRajesh 1 5,253 Dec-18-2021, 08:35 AM
Last Post: Gribouillis
  Python - Import file sequence into Media Pool jensenni 1 2,883 Feb-02-2021, 05:11 PM
Last Post: buran
Smile Import error with local file colt 1 2,512 Nov-08-2020, 07:56 AM
Last Post: Gribouillis
  Code import .CSV file to MySQL table rtakle 4 3,808 Apr-30-2020, 03:16 PM
Last Post: anbu23
  Cannot import list from another module? t4keheart 1 2,858 Feb-17-2020, 10:25 PM
Last Post: t4keheart

Forum Jump:

User Panel Messages

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