Home Backend Development Python Tutorial python基础教程之popen函数操作其它程序的输入和输出示例

python基础教程之popen函数操作其它程序的输入和输出示例

Jun 06, 2016 am 11:29 AM
enter

一、函数介绍

1.1 函数原型:

代码如下:


#include
FILE *popen(const char *command,const char *open_mode);

1.2 说明

popen函数允许一个程序将另一个程序作为新进程启动,并可以传递数据给它或者通过它接收数据。command字符串是要运行的程序名和相应参数(比如:ls或ls -l),openmode必须是 r 或w。如果是r,被调用程序的输出可以被调用它的程序使用;如果是w,调用程序就可以用fwrite向被调用程序发送数据作为它在标准输入流上的输入。

二、测试程序准备

这里准备两个很简单的程序供下面测试使用。

2.1 输出测试程序

代码如下:


// outputTest.c
#include

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

主要是实现向标准输出设备输出字符串,供下面的程序进行测试。

2.2 输入测试程序

代码如下:


// inputTest.c
#include

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

主要是实现从标准输入设备输入字符串并输出,供下面的程序进行测试。

三、popen操作示例(C代码)

3.1 获得程序输出

以outputTest程序来测试,代码如下:

代码如下:


#include
#include
#include
#include

int main()
{
        FILE *read_fp;
        char buffer[BUFSIZ + 1];
        int chars_read;
        memset(buffer,'\0',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);
}

运行效果如下:

python基础教程之popen函数操作其它程序的输入和输出示例

这里主要用r参数获得被调用程序的输出。

3.2 给其它程序传参数

以inputTest来测试,代码如下:

代码如下:


#include
#include
#include
#include

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);
}

运行效果如下:

python基础教程之popen函数操作其它程序的输入和输出示例

这里主要用w参数向被调用程序传参数。

四、poepn操作示例(python代码)

其实python也可以这样玩的。

4.1 获得程序输出

还以上文提到的outputTest程序为例,代码如下:

代码如下:


#! /usr/bin/python

import os
#var = os.popen('ls -l').read()
var = os.popen('./outputTest').read()
print var

运行效果如下:
python基础教程之popen函数操作其它程序的输入和输出示例

4.2 给其它程序传参数

还以上文提到的inputTest程序为例,代码如下:

代码如下:


#! /usr/bin/python

import os
os.popen('./inputTest','w').write("test")

运行效果如下:

python基础教程之popen函数操作其它程序的输入和输出示例
 
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Disabling Win11 Input Experience Guide Disabling Win11 Input Experience Guide Dec 27, 2023 am 11:07 AM

Disabling Win11 Input Experience Guide

Windows input encounters hang or high memory usage [Fix] Windows input encounters hang or high memory usage [Fix] Feb 19, 2024 pm 10:48 PM

Windows input encounters hang or high memory usage [Fix]

Solve win11 search bar input problem Solve win11 search bar input problem Dec 26, 2023 pm 12:07 PM

Solve win11 search bar input problem

How to input word matrix How to input word matrix Mar 19, 2024 pm 11:00 PM

How to input word matrix

C program to input an array of sequences of integers separated by spaces C program to input an array of sequences of integers separated by spaces Aug 25, 2023 am 11:33 AM

C program to input an array of sequences of integers separated by spaces

PHP Practice Guide: How to determine if the input contains only numbers and letters PHP Practice Guide: How to determine if the input contains only numbers and letters Mar 28, 2024 pm 03:06 PM

PHP Practice Guide: How to determine if the input contains only numbers and letters

What should I do if the next word I type is deleted? What should I do if the next word I type is deleted? Nov 14, 2023 am 11:56 AM

What should I do if the next word I type is deleted?

Python program: input comma separated string Python program: input comma separated string Sep 09, 2023 pm 04:25 PM

Python program: input comma separated string

See all articles