Python basic teaches the Popen function to manipulate the input and output examples of other programs _python

Source: Internet
Author: User

Introduction of function

1.1 Function Prototypes:

Copy Code code as follows:

#include <stdio.h>
FILE *popen (const char *command,const char *open_mode);

1.2 Description

The Popen function allows one program to start another program as a new process, and can pass data to it or receive data through it. The command string is the name of the program to run and the corresponding parameter (for example, LS or ls-l) and the OpenMode must be R or W. If it is r, the output of the invoked program can be used by the program in which it is invoked; if it is w, the caller can send the data to the invoked program using fwrite as its input on the standard input stream.

II. Preparation of test procedures

Here are two very simple programs to use for the test below.

2.1 Output Test Program

Copy Code code as follows:

Outputtest.c
#include <stdio.h>

int main ()
{
printf ("Just a test!") \ n ");
return 0;
}

The main is to implement the output string to the standard output device, for the following program to test.

2.2 Input Test Program

Copy Code code as follows:

Inputtest.c
#include <stdio.h>

int main ()
{
Char buf[1024] = {0};
scanf ("%s", buf);
printf ("Your input:%s\n", buf);
return 0;
}

The main is to implement the input string from the standard input device and output, for the following program to test.

Three, Popen operation example (C code)

3.1 Get program output

To test with the Outputtest program, the code is as follows:

Copy Code code as follows:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main ()
{
FILE *READ_FP;
Char Buffer[bufsiz + 1];
int chars_read;
memset (buffer, ' n ', sizeof (buffer));
READ_FP = Popen ("./outputtest", "R");
if (read_fp!= NULL)
{
Chars_read = Fread (buffer,sizeof (char), BUFSIZ,READ_FP);
if (Chars_read > 0)
{
printf ("Output was: \n%s\ndone\n", buffer);
}
Pclose (READ_FP);
Exit (exit_success);
}
Exit (Exit_failure);
}

The operation effect is as follows:


Here the output of the invoked program is obtained mainly with the R parameter.

3.2 Pass parameters to other programs

To test with Inputtest, the code is as follows:

Copy Code code as follows:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main ()
{
FILE *WRITE_FP;
Char Buffer[bufsiz + 1];

sprintf (buffer, "once...\n");
WRITE_FP = Popen ("./inputtest", "w");
if (write_fp!= NULL)
{
Fwrite (Buffer,sizeof (char), strlen (buffer), WRITE_FP);
Pclose (WRITE_FP);
Exit (exit_success);
}
Exit (Exit_failure);
}

The operation effect is as follows:

This mainly uses the W parameter to pass the parameter to the called program.

Iv. POEPN Operation Example (Python code)

In fact, Python can also play this way.

4.1 Get program Output

Also take the Outputtest procedure mentioned above as an example, the code is as follows:

Copy Code code as follows:

#! /usr/bin/python

Import OS
#var = Os.popen (' ls-l '). Read ()
var = os.popen ('./outputtest '). Read ()
Print Var

The operation effect is as follows:

4.2 Pass parameters to other programs

Also take the Inputtest procedure mentioned above as an example, the code is as follows:

Copy Code code as follows:

#! /usr/bin/python

Import OS
Os.popen ('./inputtest ', ' W '). Write ("test")

The operation effect is as follows:

Related Article

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.