From c7dd0dfdf709eae104e0e56dabf5c9781d47e1d9 Mon Sep 17 00:00:00 2001 From: rasbt Date: Fri, 1 Nov 2013 12:49:47 -0400 Subject: [PATCH] get_filename.py --- get_filename.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 get_filename.py diff --git a/get_filename.py b/get_filename.py new file mode 100644 index 0000000..a05f92a --- /dev/null +++ b/get_filename.py @@ -0,0 +1,63 @@ +# Python 2.7 +# prompt user for file of specific type(s). +# 11/01/13 sebastian raschka + +import os.path + +def get_filename(file_type): + '''repeatedly prompts user for a file of specific type. + arguments: + file_type: list with accepted file types as strings. + returns: + (string): absolute path to the specified input file. + ''' + while True: + print "\n\nplease enter a file name, \nor type --help to get"\ + " a list of the accepted file formats" + file_name = raw_input(": ") + if file_name == "--help": + print "\naccepted file format(s): ", + for f in file_type: + print f, + continue + if not os.path.isfile(file_name): + print "\n\nsorry, this file doesn't exist. please try again.\n" + continue + if not (file_name.split(".")[-1] in file_type): + print "\nplease provide a file in correct format." + continue + break + return os.path.abspath(file_name) + +#get_filename(["txt", "doc"]) + + +# =========================== +# EXAMPLE +# =========================== + +''' +[bash]~/Desktop >python get_filename.py + + +please enter a file name, +or type --help to get a list of the accepted file formats +: --help + +accepted file format(s): txt doc + +please enter a file name, +or type --help to get a list of the accepted file formats +: test.tx + + +sorry, this file doesn't exist. please try again. + + + +please enter a file name, +or type --help to get a list of the accepted file formats +: test.txt +[bash]~/Desktop > +''' +