DEV Community

Sérgio Araújo
Sérgio Araújo

Posted on

Print A Random Phrase Using Python

Some days ago I shared a little shell script to chose a random paragraph
from a study text I have. Now let's see its version using python

#!/usr/bin/env python3
#  -*- coding: utf-8 -*-
#         vim: ai ts=4 sts=4 et sw=4
#    Filename: randphrase.py
#      Author: Sergio Araujo
#     Purpose: read a random paragraph from a file
# Last Change: mai 31 2019 19:02
#     Created: sex 31 mai 2019 18:47:16 -03
#       email: <voyeg3r ✉ gmail.com>
#      Github: https://github.com/voyeg3r
#     twitter: @voyeg3r
#
# sources: 
# https://stackoverflow.com/a/40197938/2571881
# https://stackoverflow.com/a/509295/2571881

import random
import os

# clear the screen (I have used the os module just for the sake of cleaning the screen)
def cls():
    if os.name == 'posix':
        os.system('clear')
    else:
        os.system('cls')

cls()

file = "/home/sergio/.dotfiles/nvim/wiki/phrases.md"

par = open(file).read().split('\n\n')

# here I pick up a random paragraph (avoiding the two first paragraphs because they 
# are explanatory for the file and the last one wich contains tags)
print()
print(random.choice(par[2:-1]))
print()

Enter fullscreen mode Exit fullscreen mode

Top comments (0)