Learn › Python for Linux Data and Automation › Run a saved Python report

Run a Python inventory report

Edit, save and run a sample Python report as an ordinary Linux user.

A saved file becomes a running program

You will start a small Linux inventory report. Today its labels are manually entered sample text. Later lessons will teach the report to collect observations from Linux.

You need Linux Foundations skills for terminal navigation and editing with nano. You need no programming experience. Use your ordinary lab account throughout.

The terminal receives your typing and displays output. Bash, the shell in this lab, reads your shell commands and starts programs. The Python interpreter is another program. It reads Python instructions from a saved source file and follows them. The command python3 report.py asks Bash to start Python with the file report.py.

Your prepared lab folder is ~/linuxcamp/python-inventory-01. The ~ means your home folder. Inspect the starting file:

cd ~/linuxcamp/python-inventory-01
pwd
cat report.py

cd changes the current folder, pwd prints its path, and cat displays the saved file. If this folder is absent, the lab has not finished preparation. Keep that distinct from a mistake in your Python file.

Ask Python to display text

The starting source contains one line:

print("Linux inventory")

print is a function, a named operation that Python already provides. A function call asks it to do its job. The parentheses enclose what we give it.

"Linux inventory" is a string, a text value. Its matching quotes mark where that text starts and ends in the source. Both "text" and 'text' are valid quote styles.

Before running the file, make this prediction:

Inspect your real first run

Run this shell command:

python3 report.py

Look at your terminal's actual output. You should see the single line Linux inventory. That is program output. It is not another command to type. Compare it with your prediction.

The shell prompt returning means the shell is ready for another command. It does not by itself prove that a program produced the intended result. Read the output or error message.

Give a text value a name

Reports need values that can change. A variable is a name that refers to a value. This assignment gives the name machine the sample text value "lab-node":

machine = "lab-node"

Here = means assign the value on its right to the name on its left. Assignment does not print a line.

Open nano report.py. Keep the title and add the assignment and a call that displays the named value:

print("Linux inventory")
machine = "lab-node"
print(machine)

Save with Ctrl+O, press Enter to confirm the filename, then exit with Ctrl+X. Inspect the saved file with cat report.py.

Run python3 report.py and inspect both printed lines. Python follows these statements in order, so the assignment happens before the value is used.

Separate a name from literal text

Compare these two calls before running them:

print(machine)
print("machine")

The first looks up the variable's value. The second receives the literal text machine. Quotes therefore change the meaning, not just the appearance of the code.

Replace the existing final print(machine) line with these two calls. Run the saved file and explain the difference between the two output lines. Then remove print("machine") so the report keeps only the intended label.

Make the sample report understandable

An unexplained lab-node line is hard to interpret. Replace print(machine) with:

print("Machine label:", machine)

The comma separates two values given to print: a literal label and the variable's value. By default, print places one space between those values and ends the output line with a newline.

Add print("Source: manually entered sample") as the final source line. That tells a reader where the label came from. Your complete source is now:

print("Linux inventory")
machine = "lab-node"
print("Machine label:", machine)
print("Source: manually entered sample")

Predict the output order, save, then run. Compare your actual lines with that prediction. This program still does not inspect this machine's hostname or storage.

Change the report for a reason

A colleague needs a sample report labelled training-node, with a final line explaining its purpose. Change the assigned string to "training-node". Add print("Purpose: practice report") at the end. Keep the title and the sample-source line.

Before running, record in observations.md which output line should change and which should remain. Explain why changing the assignment affects the later call. Save your source, inspect it with cat report.py, then run:

python3 report.py
python3 check.py

The first command lets you inspect your own run. The supplied checker runs a copy of your saved source and compares its output with the requested report. It checks behavior; it cannot assess your prediction or explanation.

If an edit seems to have no effect, check that you saved the intended filename and are running it from the intended folder. Record the output you observed and why the sample label says nothing about the real hostname.

Read an error before changing code

Python can stop when it reaches an instruction it cannot carry out. If it reports NameError for machine, it reached a use of that name before the running program had assigned it a value. Use the message's filename and line number to find that instruction, then inspect the earlier lines.

Earlier print calls may already have displayed output before this error. Some output therefore does not prove the whole program succeeded. A SyntaxError instead identifies source that Python could not parse. Check matching quotes and parentheses before rerunning.

This reasoning will help with the return task: predict, inspect the message, justify one change, then verify both the corrected line and the lines that should stay intact.

Return to a changed report

Return after intervening study, ideally at the start of Chapter 3. The lab includes later-case.py so the task remains available. Trying it immediately is extra practice; record a later attempt separately, with its interval and any hints used.

A different team needs a handoff report for the manually entered label desk-02. Inspect later-case.py, predict what will happen, then run python3 later-case.py. Use the actual filename, message and line number to decide which single source line needs to move. Keep the title, sample-source line and purpose.

After the repair, run it again and use python3 check.py --later. Explain why the first run differed, why moving that line addresses the cause, and what the second run verifies. You may consult the explanations of assignment and execution order. The checker verifies this case's observable behavior; the explanation and delayed attempt are separate learning evidence.

The next chapter teaches numbers, text and units using clearly labelled sample values. Real Linux metadata collection follows later, after the required data and process concepts are taught.

Practice Run a Python inventory report in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.