41976

Simple. *call* function in python. How to fix the return?

Question:

See this simple code:

$ python >>> from subprocess import * >>> call(['echo','Hi']) Hi 0

My problem looks simple. I don't want this <strong>0</strong> at the end of the call. Every function called by <strong>call</strong> appears with this and this messes up things for conditional tests.

Something like:

if int(call(['function', 'parameter']))>10: print 'yes'

So how can I receive only

Hi

in this example?

Answer1:

You want to use Popen with communicate, as described in the docs:

>>> from subprocess import * >>> Popen(['echo', 'Hi'], stdout=PIPE).communicate()[0] 'Hi\n'

Answer2:

The value you are seeing is the return value of the echo process. Because you are in a active interpreter this return value gets printed to the terminal. You can ignore it by assigning it to a dummy variable:

_ = call(['echo','Hi']) <hr />

Just making sure, you do know you can just write print("Hi"), right?

Recommend

  • Always use first of default colors instead of black in ggplot2 [duplicate]
  • Jquery fadeOut on hover
  • Throwing a New Exception Best Practice [closed]
  • how to force the use of cmov in gcc and VS
  • Conditional serialization with protobuf-net
  • Add custom field for WooCommerce CSV Export plugin - For customer first order [closed]
  • Replace last two characters in column
  • How to autopopulate a field in SugarCRM form
  • Git describe fails to return most recent annotated tag
  • Differences in dis-assembled C code of GCC and Borland?
  • Unable to decode certificate at client new X509Certificate2()
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Meteor: Do Something On Email Verification Confirmation
  • Asynchronous UI Testing in Xcode With Swift
  • Get one-time binding to work for ng-if
  • Is there any way to access browser form field suggestions from JavaScript?
  • angularjs unit test when to use $rootScope.$new()
  • Running a C# exe file
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • Symfony2: How to get request parameter
  • ORA-29908: missing primary invocation for ancillary operator
  • Why winpcap requires both .lib and .dll to run?
  • align graphs with different xlab
  • Return words with double consecutive letters
  • How to set the response of a form post action to a iframe source?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Sorting a 2D array using the second column C++
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Converting MP3 duration time
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?