Processing Multiple Files in a Loop
Hardcoding file names in your code limits reusability; you have to edit the code every time you want to process a different file.
From One File to Many
A program that contains a file name directly in its code is tied to that file. To process a different file, you must edit the program and replace the old name. That makes the program less reusable. A more flexible approach asks the user for a file name while the program is running.
Following the File Name
The process has three connected steps. First, input() prompts the user for a file name. Next, the entered name is stored in a variable such as file_name. Finally, that variable is passed to open(). The variable takes the place that a literal file-name string would occupy.
Replacing the Hardcoded Name
file = open("report.txt")The program always passes the literal name report.txt to open().
file_name = input("Enter a file name: ")
file = open(file_name)The program passes the name stored in file_name to open().
Making the file choice reusable
Change a program that is tied to one file so that the user can provide the file name.
- 1Identify the fixed value
The hardcoded version gives
open()a literal file name directly in the code. - 2Ask for a name
Use
input()to prompt the user and store the entered file name infile_name. - 3Pass the variable
Give
file_nametoopen()instead of passing a literal string. - 4Reuse the program
Run the unchanged program again and provide a different file name when prompted.
The file name can change between runs without changing the program's code.
file_name = input("Enter a file name: ")
file = open(file_name)- 1The first line creates the variable
file_namefrom the user's response. - 2The second line passes that variable to
open().
input()prompts the user for a file name and stores the entered name infile_name.open()receives the value infile_name, just as it could receive a literal string.
One Program, Different Runs
In the hardcoded version, changing the target file requires editing the code. In the reusable version, the code stays the same while the user supplies a different name. The same program can therefore run multiple times on different files without code changes.
What do you think happens?
Suppose the unchanged program asks for a file name once, and you provide one file name during one run and another file name during a later run. What must be edited in the code before the second run?
Reveal answer
Nothing in the code
The user-provided name is stored in a variable and passed to open(), so a different file can be selected on another run without editing the program.
Mistakes with File Names
Common mistake Leaving the hardcoded file name in open()
What it looks likefile_name = input("Enter a file name: ") file = open("report.txt")Why it failsThe program asks for a name but then ignores the value stored in
file_name. It still passes the fixed literal name toopen().Do this insteadPass the variable instead: file = open(file_name)
Common mistake Editing the code for every new file
What it looks likeChanging open("report.txt") to open("notes.txt") before each runWhy it failsThis keeps the program tied to one hardcoded choice and removes the reusability gained from prompting the user.
Do this insteadUse input() to capture the file name while the program runs.
Common mistake Forgetting to store the input value
What it looks likeinput("Enter a file name: ") file = open(file_name)Why it failsThe later call to
open()needs the variable that contains the user-provided file name.Do this insteadAssign the result of input() to a variable, such as file_name.
Try the Pattern
Write a short Python program that asks the user for a file name, stores the response in a variable named selected_file, and passes selected_file to open(). Then run the unchanged program more than once, providing a different file name each time.
Hints
- Use
input()on the right side of an assignment. - Use the same variable name as the argument to
open(). - Do not replace the variable with a hardcoded file-name string.
Reusable File Selection
- 1A hardcoded file name forces you to edit the code when you want to process another file.
- 2
input()can prompt the user for a file name. - 3Store the response from
input()in a variable. - 4Pass that variable to
open()in the same position where a literal string could be used. - 5The unchanged program can run multiple times on different files.
Key Takeaways
- Hardcoded file names limit a program's reusability.
input()lets the user provide a file name while the program runs.- The entered name should be stored in a variable.
- Pass the variable containing the file name to
open(). - The same program can process different files across repeated runs without code changes.