0%

Commands, Configuration, and Errors · practice

Run a Python File from the Right Directory

A Python file can be sitting right there, spelled correctly, and still refuse to run. In this lesson you will run the supplied file named report.py twice with the command python report.py: once from the directory that contains it, and once from a directory that does not.

Do not edit report.py. The lesson is about how the same filename resolves from two different working directories, not about the Python code inside the file.

The beside this lesson follows your account preference. Windows opens PowerShell Core; and open Bash. Both hosted routes use the same /workspace tree and the python launcher supplied by the lab.

Run report.py from the directory that contains it

Reset the Chapter 7 workspace before you practice. The terminal starts in /workspace, and report.py sits at the top of it. A successful run prints:

Project check: ready

Before running anything, confirm both facts you are about to depend on: where you are, and that the file is there.

pwd
ls report.py
python report.py

On your own computer the launcher may be named py or python3. Ask your what resolves there; the hosted exercise deliberately uses python in both routes.

Run the same filename from the wrong directory

The command python report.py contains two separate lookups. First, the shell finds and starts python. Then Python looks for the relative filename report.py in the current working directory.

From /workspace, that filename resolves to /workspace/report.py, which exists. From /workspace/data, the same filename resolves to /workspace/data/report.py, which does not exist. Python reports the full path it tried to open.

That is the diagnostic move: compare the failed path with your current location and the file’s confirmed location before changing anything.

Practice: run report.py from two directories

Your goal is to see python report.py succeed from /workspace, see the same command fail from /workspace/data, and record the two paths for Check my work.

Use Reset before you begin so the terminal starts in /workspace and any old destination/run-report.txt is removed. Then follow these steps:

  1. Confirm your location and the supplied file from /workspace.

  2. Type python report.py. It should print Project check: ready.

  3. Move into data and confirm that your location is /workspace/data.

  4. Type python report.py again. This failure is intentional: read the full path in Python’s error message.

  5. Return to /workspace and type python report.py once more. It should work again.

  6. Create the receipt for the checker using the commands for your terminal below.

echo "attempted=/workspace/data/report.py" > destination/run-report.txt
echo "actual=/workspace/report.py" >> destination/run-report.txt
cat destination/run-report.txt

The finished receipt must contain exactly these two nonblank lines:

attempted=/workspace/data/report.py
actual=/workspace/report.py

The receipt only lets the checker validate your result; producing the successful and failed runs is the practice. Leave report.py unchanged, then press Check my work.

From /workspace/data, a learner runs python report.py. Python says it cannot open /workspace/data/report.py. The file actually exists at /workspace/report.py. Which first correction is smallest and best supported by the evidence?

From /workspace/data, which command runs report.py without moving first?

You can now follow a run command through two separate lookups: the shell finding the launcher, and the launcher finding the file. Errors have more layers than that, and the next lesson sorts them.