您在這裡

Letting the user choose the file name

23 二月, 2015 - 16:39

We really do not want to have to edit our Python code every time we want to process a different file. It would be more usable to ask the user to enter the file name string each time the program runs so they can use our program on different files without changing the Python code.

This is quite simple to do by reading the file name from the user using raw_input as follows:

fname = raw_input('Enter the file name: ')fhand = open(fname)count = 0for line in fhand:    if line.startswith('Subject:') :        count = count + 1print 'There were', count, 'subject lines in', fname

We read the file name from the user and place it in a variable named fname and open that file. Now we can run the program repeatedly on different files.

python search6.pyEnter the file name: mbox.txtThere were 1797 subject lines in mbox.txt  

python search6.pyEnter the file name: mbox-short.txtThere were 27 subject lines in mbox-short.txt

Before peeking at the next section, take a look at the above program and ask yourself, “What could go possibly wrong here?” or “What might our friendly user do that would cause our nice little program to ungracefully exit with a traceback, making us look not-so-cool in the eyes of our users?”.