Concepts / Processing Multiple Files in a Loop

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.

  • Programming

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

Userfile nameinput()promptfile_nameuser-provided nameopen()file name argumententersstorespasses
How does the file name move from the user's input into a variable and then into the open() function?

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

Before
file = open("report.txt")

The program always passes the literal name report.txt to open().

After
file_name = input("Enter a file name: ")
file = open(file_name)

The program passes the name stored in file_name to open().

Worked example

Making the file choice reusable

Change a program that is tied to one file so that the user can provide the file name.

  1. 1
    Identify the fixed value

    The hardcoded version gives open() a literal file name directly in the code.

  2. 2
    Ask for a name

    Use input() to prompt the user and store the entered file name in file_name.

  3. 3
    Pass the variable

    Give file_name to open() instead of passing a literal string.

  4. 4
    Reuse the program

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

Result

The file name can change between runs without changing the program's code.

Code walkthrough
file_name = input("Enter a file name: ")
file = open(file_name)
  1. 1
    The first line creates the variable file_name from the user's response.
  2. 2
    The second line passes that variable to open().
  • input() prompts the user for a file name and stores the entered name in file_name.
  • open() receives the value in file_name, just as it could receive a literal string.

One Program, Different Runs

Programfixed file namereport.txtliteral stringProgramuser-provided file namefile_namevariablepassespasses
What changes in the program when the file name is hardcoded compared with when the program asks the user for a file name?

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?

  • The file-processing code

  • The open() call

  • Nothing in the code

  • The variable name

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

  • Common mistake

    Leaving the hardcoded file name in open()

    What it looks like
    file_name = input("Enter a file name: ")
    file = open("report.txt")
    Why it fails

    The program asks for a name but then ignores the value stored in file_name. It still passes the fixed literal name to open().

    Do this instead

    Pass the variable instead: file = open(file_name)

  • Common mistake

    Editing the code for every new file

    What it looks like
    Changing open("report.txt") to open("notes.txt") before each run
    Why it fails

    This keeps the program tied to one hardcoded choice and removes the reusability gained from prompting the user.

    Do this instead

    Use input() to capture the file name while the program runs.

  • Common mistake

    Forgetting to store the input value

    What it looks like
    input("Enter a file name: ")
    file = open(file_name)
    Why it fails

    The later call to open() needs the variable that contains the user-provided file name.

    Do this instead

    Assign the result of input() to a variable, such as file_name.

Try the Pattern

EASY

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

  1. 1
    A hardcoded file name forces you to edit the code when you want to process another file.
  2. 2
    input() can prompt the user for a file name.
  3. 3
    Store the response from input() in a variable.
  4. 4
    Pass that variable to open() in the same position where a literal string could be used.
  5. 5
    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.

Related concepts

More concepts starting with P