Python Forum
Import random from a txt file
Thread Rating:
  • 3 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Import random from a txt file
#1
Hey guys, I would just like to know how you import a random selection from a list that has been imported into python via a txt file as im trying to make like a dvd menu movie library thingy

I am trying to import a file from a folder then print a random title from the txt file but I get this strange output

here is my code

import random
print("***Movie Title Explorer***")
print("l – load file of movie titles"'\n'"r – random movie"'\n'"s– search"'\n'""
      "sw – starts with"'\n'"k – keep - save the last displayed movie title to your favourites"'\n'"f – favourites display"'\n'""
      "c – clear"'\n'"q - quit program")
print("command = ?")
selection=input("Please Select:")
if selection =='l':
    file = open("C:\\Users\\LiquidO\\Desktop\\Assingment 2 python\\movies.txt", "r")
    movies_list = file.readlines() # load each line into a list
    file.close() # close the file
    movies_list = [movie.strip() for movie in movies_list]
    print("Movies now loaded")
selection = input("Please Select:")
if selection =='r':
    print(random.choice([file]))
***Movie Title Explorer***
l – load file of movie titles
r – random movie
s– search
sw – starts with
k – keep - save the last displayed movie title to your favourites
f – favourites display
c – clear
q - quit program
command = ?
Please Select:l
Movies now loaded
Please Select:r
<_io.TextIOWrapper name='C:\\Users\\LiquidO\\Desktop\\Assingment 2 python\\movies.txt' mode='r' encoding='cp1252'>

Process finished with exit code 0

My bad I forgot to take out line 11 and also make line 16's string movies_list not file lol
    file.close() # close the file
Although I can now print the list it seems it still prints the whole contents of the txt file. Any idea how to print just one random item from the list rather than the whole lot?

import random
print("***Movie Title Explorer***")
print("l – load file of movie titles"'\n'"r – random movie"'\n'"s– search"'\n'""
      "sw – starts with"'\n'"k – keep - save the last displayed movie title to your favourites"'\n'"f – favourites display"'\n'""
      "c – clear"'\n'"q - quit program")
print("command = ?")
selection=input("Please Select:")
if selection =='l':
    file = open("C:\\Users\\LiquidO\\Desktop\\Assingment 2 python\\movies.txt", "r")
    movies_list = file.readlines() # load each line into a list
    movies_list = [movie.strip() for movie in movies_list]
    print("Movies now loaded")
selection = input("Please Select:")
if selection =='r':
    print(random.choice([movies_list]))
Reply


Messages In This Thread
Import random from a txt file - by Liquid_Ocelot - May-05-2017, 08:47 PM
RE: Import random from a txt file - by snippsat - May-05-2017, 09:56 PM
RE: Import random from a txt file - by ichabod801 - May-05-2017, 10:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Frequency in first digit from csv file, NO IMPORT bryan0901 6 2,910 May-28-2020, 09:50 AM
Last Post: bryan0901
  Import Python file with a name of a module in Stantard Library tiago_code 7 3,219 Dec-11-2019, 06:08 AM
Last Post: ICanIBB
  Import a Data from a text file maxcom 1 2,449 Apr-01-2018, 07:30 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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