简体   繁体   中英

call a function in python script then check if condition

I have this function:

def ContentFunc():
        storage = StringIO()
        c = pycurl.Curl()
        c.setopt(c.URL, url)
        c.setopt(c.WRITEFUNCTION, storage.write)
        c.perform()
        c.close()
        content = storage.getvalue()


while True:
        ContentFunc()
        if "word" in content:
             out = open('/tmp/test', 'a+')

I want to append content from content = storage.getvalue() . But doesn't work.

The ERROR:

NameError: name 'content' is not defined

Can you help me?

In your function

def ContentFunc():
    ...
    content = storage.getvalue()

This defines content within the scope of that function . The function then ends, and that name (and the object assigned to it) is discarded. Instead, return from the function:

def ContentFunc():
    ...
    return storage.getvalue()

and assign the name in the calling function:

content = ContentFunc()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM