Using the input() Function to Capture User Input
Hardcoding file names in your code limits reusability; you have to edit the code every time you want to process a different file.
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.
Identify the fixed part: A hardcoded file name is tied to one file. Processing another file requires editing the source code.
Capture the choice: Use input() to prompt the user for a file name and store the response in the variable file_name.
Pass the variable: Give file_name to open(). The variable now supplies the file name that open() uses.
Reuse 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)
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.
Mistakes That Break Reusability
Leaving the file name hardcoded
The program remains tied to the fixed file name, so another file requires a source-code edit.
Fix:
Use input() to capture a file name in a variable and pass that variable to open().Capturing the response but passing a literal to open()
The user's response is stored, but open() still receives the fixed literal file name.
Fix:
Pass file_name to open() so the user's selected name is used.Expecting a different file choice without running the prompt
The variable must contain a file name captured by input() before it can provide the user-selected name to open().
Fix:
First 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
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
- A hardcoded file name limits reusability because another file requires a code edit.
- input() prompts the user for a file name and stores the response in a variable.
- The variable containing the file name can be passed to open() just as a literal file name can.
- The same unchanged program can run multiple times on different files.
- The 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.