Open files from a folder with Python and os.listdir()

This time we want to create a little script to search for all the files in a directory, ask you to pick up one to be opened with the associated program in your OS.

import os

def menu():
	for n,each in enumerate(os.listdir()):
		print(n, each)
	ask = int(input("Number? > "))
	os.startfile(os.listdir()[ask])

if __name__ == "__main__":
	menu()



Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.