Concepts / Using the input() Function to Capture User Input

Using the input() Function to Capture User Input

On one run, the user supplies one file name.

  • Programming

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().

Programfixed file nameFile AProgramuser-provided nameSelected filealways usesuses at runtime
What changes between a program that contains a fixed file name and one that accepts a file name at runtime?

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.

Userfile name responseinput()promptfile_namestored responseopen()file name argumentprovides namestores responsepasses variable
How does a file name move from the user's response into a variable and then into open()?
python

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

Program startsinput()prompt for file namefile_nameuser responseopen()variable as argumentSelected filerunscapturessupplies nameopens
What happens next when the program prompts the user, receives a file name, and opens that file?
Worked example

Replacing a Hardcoded Name

Make a file-opening program usable with a file name chosen by the user.

  1. 1
    Identify the fixed part

    A hardcoded file name is tied to one file. Processing another file requires editing the source code.

  2. 2
    Capture the choice

    Use input() to prompt the user for a file name and store the response in the variable file_name.

  3. 3
    Pass the variable

    Give file_name to open(). The variable now supplies the file name that open() uses.

  4. 4
    Reuse the program

    Run the unchanged program again and provide a different file name when prompted.

Result

The file name is selected at runtime, so the same program can run on different files without code changes.

Code walkthrough
file_name = input("Enter a file name: ")
file = open(file_name)
  1. 1
    The first line uses input() to ask the user for a file name.
  2. 2
    The response is assigned to file_name, so the program has a variable containing the user's chosen name.
  3. 3
    The second line passes file_name to open(), allowing open() to use the user-provided name.
  4. 4
    Because 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.

ProgramunchangedFile AProgramunchangedFile Buser suppliesuser supplies
How can the same unchanged program process different files on separate runs?
Before
file = open("file_a.txt")

The file name is fixed in the source code.

After
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 like
    file = open("file_a.txt")
    Why it fails

    The program remains tied to the fixed file name, so another file requires a source-code edit.

    Do this instead

    Use 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 like
    file_name = input("Enter a file name: ")
    file = open("file_a.txt")
    Why it fails

    The user's response is stored, but open() still receives the fixed literal file name.

    Do this instead

    Pass 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 like
    file = open(file_name)
    Why it fails

    The variable must contain a file name captured by input() before it can provide the user-selected name to open().

    Do this instead

    First assign the result of input() to the variable, then pass that variable to open().

Best practice

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

EASY

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

  1. 1
    A hardcoded file name limits reusability because another file requires a code edit.
  2. 2
    input() prompts the user for a file name and stores the response in a variable.
  3. 3
    The variable containing the file name can be passed to open() just as a literal file name can.
  4. 4
    The same unchanged program can run multiple times on different files.
  5. 5
    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.

Related concepts

More concepts starting with U