71422

call a function in python script then check if condition

Question:

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?

Answer1:

In your function

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

This defines content <em>within the scope of that function</em>. 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()

Recommend

  • TLS v1.1 / TLS v1.2 support in RCurl
  • What is the proper method for creating a bindable property on a user control in WPF?
  • Multiple Thread Variable Access inside a function
  • How to paginate a Firebase Recyclerview
  • SpinCtrl with step increment value
  • How to get a slider's value in Qualtrics using jQuery?
  • Custom Map XmlAdapter
  • Access Amazon s3 using http in angular2
  • How to remove the marker in Google map v2?
  • Unique ID counter
  • lodash _.get function in typescript
  • How can I use 'addChildEventListener()' accurately for following purpose?
  • JavaFX TableView click sorters not working?
  • Get current day's steps during datapointListener google Fit
  • How to prevent TreeItem selection?
  • Mysql - How to search for 26 records that each begins with the letter of the alphabet?
  • Unable to get column index with table.getColumn method using custom table Model
  • Declaring variable dynamically in VB.net
  • Transactional Create with Validation in ServiceStack Redis Client
  • Hardware Accelerated Image Scaling in windows using C++
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • TFS: Get latest causes slow project reloading
  • Running a C# exe file
  • Join two tables and save into third-sql
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Circular dependency while pushing http interceptor
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • Suggestions to manage Login/Logout transitions
  • How to set the response of a form post action to a iframe source?
  • coudnt use logback because of log4j
  • How do I configure my settings file to work with unit tests?
  • Change div Background jquery
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • How can I use `wmic` in a Windows PE script?