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
Making the file choice reusable
Change a program that is tied to one file so that the user can provide the file name.
Identify the fixed value: The hardcoded version gives open() a literal file name directly in the code.
Ask for a name: Use input() to prompt the user and store the entered file name in file_name.
Pass the variable: Give file_name to open() instead of passing a literal string.
Reuse 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)
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
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
Leaving the hardcoded file name in open()
The program asks for a name but then ignores the value stored in file_name. It still passes the fixed literal name to open().
Fix:
Pass the variable instead: file = open(file_name)Editing the code for every new file
This keeps the program tied to one hardcoded choice and removes the reusability gained from prompting the user.
Fix:
Use input() to capture the file name while the program runs.Forgetting to store the input value
The later call to open() needs the variable that contains the user-provided file name.
Fix:
Assign 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
- A hardcoded file name forces you to edit the code when you want to process another file.
- input() can prompt the user for a file name.
- Store the response from input() in a variable.
- Pass that variable to open() in the same position where a literal string could be used.
- The 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.