8.6. Letting the user choose the file name

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 input as follows:

fname = input('Enter the file name: ')
fhand = open(fname)
count = 0
for line in fhand:
    if line.startswith('Subject:'):
        count = count + 1
fhand.close()
print('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.py # seach6.py is the file containing the above script.
 Enter the file name: mbox.txt
 There were 1797 subject lines in mbox.txt

 python search6.py
  Enter the file name: mbox-short.txt
There were 27 subject lines in mbox-short.txt

Put the following code in order to open and count the lines of a file from the user. Watch out for indentation and extra code blocks!

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?”

There are five errors in the code below. Fix the code to ask a user for a file, open “mbox-short.txt”, and count the lines that start with Received:.

You have attempted of activities on this page