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?
- 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
- BThe script uses incorrect conditional operator syntax; numerical comparisons against environment variables require Unix-style operators such as -gt instead of relational symbols.
- CEnvironment variables in Python are limited to process-local scope; the variable must be declared with the export keyword inside the script body before comparison.
- DPython 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
Key Concept
Environment Variables and Data Type Casting in Scripting
Estimated Time:2m 0s