• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Read Random Line from File

Using Python to Read Random Line from File

May 16, 2022 Leave a Comment

To read a random line from a file in Python, you can use the Python random module with the read() and splitlines() functions.

import random

with open("example.txt","r") as file:
    lines = file.read().splitlines()
    print(random.choice(lines))

When working with files, the ability to easily extract different pieces of information can be very valuable.

One such piece of information is the ability to get a random line from a file.

We can use the Python random module to help us get a random line from a file.

To get all of the lines in a file, first use the read() and splitlines() functions.

Then, you can use the random.choice() function to get a random line from the file.

Below is a simple example showing you how to get a random line from a file in Python.

import random

with open("example.txt","r") as file:
    lines = file.read().splitlines()
    print(random.choice(lines))

Reading Multiple Random Lines from File Using Python

If you want to read multiple random lines from file in Python, we can make a slight modification to the code from above.

Let’s say for example you want to read a handful of lines randomly from a file using Python.

Instead of using random.choice(), you should use random.sample() and pass the number of lines you want to read.

Below is an example showing you how to read multiple lines randomly from a file with Python.

import random

with open("example.txt","r") as file:
    lines = file.read().splitlines()
    print(random.sample(lines,5))

Hopefully this article has been useful for you to understand how to read a file and get a random line from that file using Python.

Other Articles You'll Also Like:

  • 1.  Find Index of Maximum in List Using Python
  • 2.  Using Python to Sum Even Numbers in List
  • 3.  Check if Variable is String in Python
  • 4.  Python if else do nothing – Using pass Statement to Do Nothing in Python
  • 5.  Get Tomorrow’s Date as Datetime in Python
  • 6.  Using Python to Add Items to Set
  • 7.  Create Empty List in Python
  • 8.  Truncate String in Python with Slicing
  • 9.  Get Month Name from Datetime in pandas DataFrame
  • 10.  Write Integer to File Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy