在python脚本中调用一个函数,然后检查条件 [英] call a function in python script then check if condition
本文介绍了在python脚本中调用一个函数,然后检查条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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()
而真:
ContentFunc()
如果内容中包含word:
out = open('/ tmp / test','a +')
我想追加 content from
content = storage.getvalue()
。但不起作用。
错误:
NameError: name'content'没有定义
你能帮我吗?
<在你的函数中,
$ p $ def ContentFunc():
...
content = storage.getvalue()
定义 content
在该函数的范围内。该函数然后结束,并且该名称(以及分配给它的对象)将被丢弃。相反,
def ContentFunc():
...
返回storage.getvalue()
调用函数:
content = ContentFunc()
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()
这篇关于在python脚本中调用一个函数,然后检查条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文