Using the input() Function to Capture User Input
On one run, the user supplies one file name.
From Fixed Script to Reusable Tool
A program that contains one fixed file name can process that file, but it is less reusable. To process a different file, you have to edit the code. The input() function changes this pattern: the program asks the user for a file name while it is running, stores the response in a variable, and passes that variable to open().
The Input-to-Open Path
The reusable pattern has three connected steps. First, input() prompts the user for a file name. Second, the response is stored in a variable. Third, that variable is passed to open() in the same way that a literal file-name string could be passed. The variable acts as the connection between the user's response and the file-opening operation.
In this example, file_name is the variable that stores the user's response. The same variable is then supplied to open(). The important change is not the particular name of the variable; it is that open() receives the stored user-provided file name instead of a fixed file name written directly into the program.
Program Flow at Runtime
Replacing a Hardcoded Name
Make a file-opening program usable with a file name chosen by the user.
- 1Identify the fixed part
A hardcoded file name is tied to one file. Processing another file requires editing the source code.
- 2Capture the choice
Use
input()to prompt the user for a file name and store the response in the variablefile_name. - 3Pass the variable
Give
file_nametoopen(). The variable now supplies the file name thatopen()uses. - 4Reuse the program
Run the unchanged program again and provide a different file name when prompted.
The file name is selected at runtime, so the same program can run on different files without code changes.
file_name = input("Enter a file name: ")
file = open(file_name)- 1The first line uses
input()to ask the user for a file name. - 2The response is assigned to
file_name, so the program has a variable containing the user's chosen name. - 3The second line passes
file_nametoopen(), allowingopen()to use the user-provided name. - 4Because the name is supplied while the program runs, the source code does not need to change when a different file is selected.
- Prompts the user and stores the response in
file_name. - Passes the stored file name to
open().
Reuse Across Separate Runs
The source code can stay unchanged across separate runs. On one run, the user supplies one file name. On another run, the user supplies a different file name. In both cases, input() captures the name, the variable stores it, and open() receives that variable.
file = open("file_a.txt")The file name is fixed in the source code.
file_name = input("Enter a file name: ")
file = open(file_name)The file name is supplied while the program runs.
Mistakes That Break Reusability
Common mistake Leaving the file name hardcoded
What it looks likefile = open("file_a.txt")Why it failsThe program remains tied to the fixed file name, so another file requires a source-code edit.
Do this insteadUse input() to capture a file name in a variable and pass that variable to open().
Common mistake Capturing the response but passing a literal to open()
What it looks likefile_name = input("Enter a file name: ") file = open("file_a.txt")Why it failsThe user's response is stored, but
open()still receives the fixed literal file name.Do this insteadPass file_name to open() so the user's selected name is used.
Common mistake Expecting a different file choice without running the prompt
What it looks likefile = open(file_name)Why it failsThe variable must contain a file name captured by
input()before it can provide the user-selected name toopen().Do this insteadFirst assign the result of input() to the variable, then pass that variable to open().
When a file name may vary from one run to the next, keep that choice outside the source code. Prompt for it with input(), give the response a variable name, and pass the variable to open(). This separates the reusable program instructions from the particular file selected for a run.
Practice the Pattern
Rewrite a file-opening statement that uses a fixed file name so that the program asks the user for a file name and passes the user's response to open(). Then describe what can change between two runs and what stays unchanged.
Hints
- Create a variable by assigning
input()to it. - Use the same variable as the argument to
open(). - The source code should not need to change when the user selects a different file.
What do you think happens?
If the program contains input() and passes its stored response to open(), what must you edit in the source code to process a different file on the next run?
Reveal answer
Nothing in the source code needs to change; provide a different file name when the program prompts you.
The file name is selected at runtime, stored in a variable, and passed to open(). This removes the need to edit a hardcoded file name for each different file.
Reusable File Selection
- 1A hardcoded file name limits reusability because another file requires a code edit.
- 2
input()prompts the user for a file name and stores the response in a variable. - 3The variable containing the file name can be passed to
open()just as a literal file name can. - 4The same unchanged program can run multiple times on different files.
- 5The key sequence is prompt, store, and pass.
Key Takeaways
- Hardcoding makes a program dependent on one file name and requires source-code edits for another file.
- Use
input()to ask the user for a file name while the program runs. - Store the response in a variable and pass that variable to
open(). - The same program can then run on different files without code changes.