Python by example 1: get the content of a text file with a cli

This code will ask the user to choose among some text files in the directory and it will return the content of the text file.

# Brought to you by: pythonprogramming.altervista.org
from glob import glob


def read_file():
    "Show text file and ask user to choose 1 -> content as text"
    elenco = glob("*.txt")
    [print(n, x) for n, x in enumerate(elenco)]
    num = int(input("Quale scegli?\n"))
    with open(elenco[num], "r", encoding="utf-8") as file:
        file = file.read()
    return file


print(read_file())


Subscribe to the newsletter for updates
Tkinter templates
My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts