I am a beginner with Python. I have a script in which I want to check if the Eastern Timezone is currently in daylight savings time:
import sys
import os
import time
os.environ['TZ'] = 'America/Toronto'
time.tzset()
time.localtime()
isDST = _.tm_isdst
I get the following error:
Traceback (most recent call last):
File "./myPythonScript.py", line 20, in <module>
isDST = _.tm_isdst
NameError: name '_' is not defined
If I open up the python interpreter, I can enter in the script code a line at a time and everything works fine. But when I try to run the script I get the error. Does anyone have any ideas or suggestions?
I’m running Python 2.5.2.
time.localtime()returns atime.struct_timeobject that you should store in a variable:You can then look at its attributes by referencing them by name:
The
_is provided automatically by the interactive interpreter for convenience, but it’s not provided when you’re running a script.