Member Avatar for sravan953

Hey guys,

I have some code:

import urllib2 as url
import subprocess
import os

webs=open("url.txt",'r')
read_webs=webs.read()

def get_html():
    try:
        req=url.urlopen(read_webs)
        res=req.read()
        return True
    except:
        print("<Error reading from the website>")

def get_command():
    if True:
        for x in res:
            if(x!='<'):
                command+=x
            else:
                break
    determine()

command=""

def determine():
    format=command[len(command)-3:]
    print format

get_html()
get_command()

The problem is:

>>>
Traceback (most recent call last):
File "C:\Documents and Settings\Sravan\My Documents\Sravan\PYTHON\Accepshun.py", line 32, in <module>
get_command()
File "C:\Documents and Settings\Sravan\My Documents\Sravan\PYTHON\Accepshun.py", line 18, in get_command
for x in res:
NameError: global name 'res' is not defined
>>>

I understand that get_command() doesn't know what 'res' is, but how do I solve this problem?

Try to use 'res' as an argument passed between your functions ...

import urllib2 as url
import subprocess
import os

webs=open("url.txt",'r')
read_webs=webs.read()

def get_html():
    try:
        req=url.urlopen(read_webs)
        res=req.read()
        return res
    except:
        #print("<Error reading from the website>")
        return False

def get_command(res):
    if True:
        for x in res:
            if(x!='<'):
                command+=x
            else:
                break
    determine()

command=""

def determine():
    format=command[len(command)-3:]
    print format

res = get_html()
if res:
    get_command(res)
else:
    print("<Error reading from the website>")
Member Avatar for sravan953

vegaseat, can you explain to me

res = get_html()
if res:
    get_command(res)
else:
    print("<Error reading from the website>")

How exactly does res=get_html() work?

vegaseat, can you explain to me

res = get_html()
if res:
    get_command(res)
else:
    print("<Error reading from the website>")

How exactly does res=get_html() work?

the funktion returns res, he did that at line 10 where it returns res
its like i tell u to do something and then you give me the result or something

def re():
    return "hello"
string = re()
print string

so it takes re and returns so if you would do print get_html() it would print res
im not sure how to explain netter >_>

Member Avatar for sravan953

Thanks Friktion, I got it! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.