R
RiGGa
Hi,
I am having problems getting my script to recognize global variables and
each time I run the script I always get this warning:
SyntaxWarning: name 'getdata' is assigned to before global declaration
The structure of my script is below:
===========================================================================
import urllib
import sys
global myvariable
myvariable = 0
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
'Do some stuff here and reassign a value to myvariable'
def handle_data(self, data):
if myvariable == 1:
'Do some more stuff here'
if __name__ == "__main__":
try:
file = sys.argv[1]
fd = open(file)
except IOError, detail:
print "Couldn't open file '%s' for reading!"%file, detail[1]
sys.exit(1)
except IndexError:
print "No filename given!"
sys.exit(2)
my_parser=MyHTMLParser() # Instantiate my Parser
my_parser.feed(fd.read()) # Feed it
my_parser.close() # Close the parser.
fd.close() # Close the file.
sys.exit(0)
===========================================================================
It appears to totally ignore the fact that I have specified global
myvariable. if i do not specify global at all I get an error stating:
UnboundLocalError: local variable 'myvariable' referenced before assignment
What am I doing wrong?? (Im a Python newbie so be gentle!)
Thanks
Rigga
I am having problems getting my script to recognize global variables and
each time I run the script I always get this warning:
SyntaxWarning: name 'getdata' is assigned to before global declaration
The structure of my script is below:
===========================================================================
import urllib
import sys
global myvariable
myvariable = 0
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
'Do some stuff here and reassign a value to myvariable'
def handle_data(self, data):
if myvariable == 1:
'Do some more stuff here'
if __name__ == "__main__":
try:
file = sys.argv[1]
fd = open(file)
except IOError, detail:
print "Couldn't open file '%s' for reading!"%file, detail[1]
sys.exit(1)
except IndexError:
print "No filename given!"
sys.exit(2)
my_parser=MyHTMLParser() # Instantiate my Parser
my_parser.feed(fd.read()) # Feed it
my_parser.close() # Close the parser.
fd.close() # Close the file.
sys.exit(0)
===========================================================================
It appears to totally ignore the fact that I have specified global
myvariable. if i do not specify global at all I get an error stating:
UnboundLocalError: local variable 'myvariable' referenced before assignment
What am I doing wrong?? (Im a Python newbie so be gentle!)
Thanks
Rigga