I want to run a script over a .txt file in a given directory. Files in the folder will change and I'm interested in listing them and picking one via keyboard entry.
This is the code I have so far,
import os
folder = os.getcwd() #Current directory
files = [f for f in os.listdir(folder) if f.endswith('.txt')]
print 'Files available:\n %r' % files
Which would give me an output with the list of files that I could analyze.
Sth like this,
Files available: ['File.txt', 'Foo.txt', 'Test.txt']
And now the piece of code in which I'm stuck:
while True:
print "Introduce the name of the file"
choice = raw_input()
if choice.lower() == 'FILE FROM THE LIST':#Here's where I'm stuck
with open('FILE FROM THE LIST', 'rt') as inputfile:
data = csv.reader(inputfile)
#Do stuff
elif choice.lower() == 'e':
break
else:
print "Please, choose a valid option or type 'e' to exit"
What should I do to type the name of the file and running the script from there?
Ideally I would like to create a link between the listed files and a key or a number to make it shorter, e.g.
[Type '1' to open File.txt,
Type '2' to open Foo.txt,
Type '3' to open 'Text.txt',...]
But typing the name would be a nice way to begin for me.