笔记:python 调用 shell 和 C
2015-04-04 13:30
267 查看
python 调用 C :
还是shell 获得linux 相关比较简单。
目前发现cmd好用。但是不适合很长的shell命令。只能写在脚本哩。。。[最好用的方法应该是???
pythhon 调用C 的时候,向C传递的参数=======发现直接传int不对。。。字符串接收是可以的。
C++ 编译为so 供 python调用,命令:
g++ perm.cpp -fPIC -shared -o perm.so
python
python 代码:
shell:
C代码:
还是shell 获得linux 相关比较简单。
目前发现cmd好用。但是不适合很长的shell命令。只能写在脚本哩。。。[最好用的方法应该是???
pythhon 调用C 的时候,向C传递的参数=======发现直接传int不对。。。字符串接收是可以的。
C++ 编译为so 供 python调用,命令:
g++ perm.cpp -fPIC -shared -o perm.so
python
python 代码:
import sys import os import glob import subprocess import shutil import traceback import fcntl import signal import ConfigParser import time def get_files(): global file cmd = ["sh", "./test.sh"] process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) out = process.communicate() if process.returncode == 0 and out[0].strip() != "NULL": """ print out for i in range(len(out)): print out[i].strip()""" number_i = out[0].strip() print number_i import ctypes so = ctypes.CDLL("./perm.so") ret = so.v_perm_v(number_i) print("the return value is:") print ret def main(argv=None): """creates a parser for the command line arguments and runs the program for those arguments""" get_files() if __name__ == "__main__": sys.exit(main()) ~
ls /home/vivi | wc -l
shell:
ls /home/vivi | wc -l
C代码:
#include <stdio.h> #include <stdlib.h> #define elemType int void arrySwap(elemType arr[], elemType a, elemType b) { elemType temp; temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } int cnt = 0; void perm(elemType arr[], int start, int end) { if(start == end) { cnt++; printf("%d :/t",cnt); for(int i = 0; i <= end; i++) printf("%d ", arr[i]); printf("\n"); } else { for(int i = start; i <= end; i++) { arrySwap(arr, start, i); perm(arr, start + 1, end); arrySwap(arr, start, i); } } } extern "C" { int v_perm_v(char* len_c) { //printf("len_c = %s\n",len_c); int len = (int)(len_c[0] - '0'); //printf("len = %d\n", len); if(len <= 0) return -1; int *arr = (int *)malloc(sizeof(int) * len); if(arr == NULL) return -1; for(int i = 0; i < len; i++) { arr[i] = i; //printf("%d ", arr[i]); } perm(arr, 0, len - 1); return 0; } }
相关文章推荐
- python 学习笔记 8 -- Python下调用Linux的Shell命令
- python 学习笔记 8 -- Python下调用Linux的Shell命令
- Python笔记(二)——python调用C/C++模块
- 一个有用函数:python调用shell命令
- python调用shell命令
- python调用Shell脚本:os.system(cmd)或os.popen(cmd)【转】
- python中调用shell
- Python笔记(一)——C/C++调用python
- python调用Shell脚本
- python调用Shell脚本:os.system(cmd)或os.popen(cmd)
- python 调用shell命令三种方法
- python 调用shell命令三种方法【转贴】_小妮子8308-ChinaUnix博客
- python中shell调用py文件的内容
- python调用Shell脚本:os.system(cmd)或os.popen(cmd)()【转】
- Python中如何调用shell命令
- python 调用Linux系统命令笔记
- Python笔记(二)――python调用C/C++模块
- python调用Shell脚本:os.system(cmd)或os.popen(cmd)
- awk 笔记 2 调用shell命令
- python 如何调用带参数的shell脚本