Python's method of calling executable file __python

Source: Internet
Author: User

Recently I used Python to call C programs, so I looked at the way Python calls other programs. Generally speaking, Python calls C/C + + There are two ways, one is to invoke the dynamic link library, that is, the so file, a C-generated executable file. Specifically, depending on the application scenario.

Python invokes the executable file, and in fact executes the commands that were originally executed on the command line in Python.

Specific methods:

(1). Write C + + program with main program MAIN.C

#include <stdio.h>
#include <stdlib.h>
int main () {
    int a = ten;
    int b = 3;


    int c = A * b;
	printf ("%d\n", c);
    return 0;
}

Save the program and compile it into an executable file with GCC Calledbypy.

(2). Python Calling Program

(This program is from http://www.cnblogs.com/apexchu/p/5015961.html)

Import commands  
import os  
main = "./calledbypy"  
if Os.path.exists (main):  
    rc, out = Commands.getstatusoutput (main)  
    print ' rc =%d, \nout =%s '% (RC, out)  
  
print ' * ' *10  
f = os.popen (main)    
dat A = F.readlines ()    
f.close ()    
print data  
  
print ' * ' *10  
r_v = Os.system (main) 


The results after execution are:

rc = 0,
out = 30
**********
[' 30\n ']
**********
30
0

There are 3 ways to invoke an executable file in a Python program. commands, Popen and Os.system. From the above output can be seen:

Commands.getstatusoutput () Saves the print value and the return value of the main function in the executable program, but does not print the output in the execution of the command, and then writes the print statement.

Popen prints all of the printed content in the executable program to a file, does not print the content to be output during execution, and can output all the printed content by reading the file, and Os.popen does not save the return result of the main function;

Os.system () saves the printed value and the return result of the main function, and prints the content to be printed during execution.


As mentioned above, these three methods are actually executing commands in Python, so they are not just used to execute executable files, they can also be used to execute other instructions on the Linux system.

For example

Import commands
import os


main = "ls"
rc, out = Commands.getstatusoutput (main)
print RC
print out< C5/>print ' * ' *
f = os.popen (main)
data = F.readlines ()
f.close ()
print data


print ' * '
* R_v = Os.system (main)
print R_v
 
The output results are:

0
Calledbypy
Calledbypy.c
Makefile
pycallcpp.py
**********
[' calledbypy\n ', ' calledbypy.c\n ', ' makefile\n ', ' pycallcpp.py\n ']
**********
Calledbypy
Calledbypy.c
Makefile
pycallcpp.py
0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.