Question

Difficulty: Very hardBasic Scripting Languages and Constructs

An IT technician is reviewing a Python (.py) automation script deployed to process system log files across multiple workstations. The script reads a configuration threshold from an operating system environment variable to determine when to trigger log compression. The variable is retrieved into the script, but when the script compares this environment variable against an integer variable representing the current file size in megabytes (log_size_mb), the conditional statement raises a runtime TypeError exception and halts execution. Which of the following identifies the root cause of this failure and the correct solution?

  1. Environment variables are retrieved as string data types by default; the value must be explicitly cast to an integer before conducting a numeric comparison.Answer
  2. B
    The script uses incorrect conditional operator syntax; numerical comparisons against environment variables require Unix-style operators such as -gt instead of relational symbols.
  3. C
    Environment variables in Python are limited to process-local scope; the variable must be declared with the export keyword inside the script body before comparison.
  4. D
    Python environment variables require a leading dollar sign ($) prefix; omitting the prefix causes the runtime engine to evaluate the variable name as an uninitialized object.

Answer

Environment variables are retrieved as string data types by default, requiring explicit casting to an integer using int() prior to numeric conditional evaluation.
In system scripting, environment variables stored by the operating system are treated strictly as string data types when imported into a script. In Python (.py), attempting to compare an integer variable with a string variable using relational operators (such as > or <) results in a runtime TypeError. The technician must explicitly convert (cast) the string retrieved from the environment variable into an integer using the int() function before evaluating the condition.

Step-by-Step Solution

1
Analyze the reported error type and script behavior.
A TypeError during relational comparison (<, >) indicates a data type mismatch between the two operands being evaluated.
Python does not automatically coerce string objects into integers during relational comparisons.
2
Identify the data type returned by environment variable constructs across scripting environments.
Operating system environment variables are stored and returned as string data types regardless of whether their contents represent numeric digits.
Scripting runtimes parse OS environment values as text strings by default.
3
Determine the necessary syntax construct to resolve the type mismatch.
Casting the environment variable using integer conversion syntax (e.g., int(os.environ['MAX_LOG_SIZE'])) aligns data types for valid comparison.
Explicit type casting allows numerical conditional constructs to execute properly without raising runtime exceptions.

Key Concept

Environment Variables and Data Type Casting in Scripting
Estimated Time:2m 0s
Rate this question