
正文
Python函数proc python函数print
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Python中执行系统命令常见的几种方法
作为胶水语言,Python可以很方便地执行系统命令,从而帮助我们快速的完成任务;而且Python执行系统命令可采用的方法有很多,本文重点介绍一下:os.system()、os.popen()和subprocess模块。
1. os.system()
这个方法直接调用标准C的system()函数,仅仅在一个子终端运行系统命令,而不能获取执行返回的信息。
import os
output = os.system('cat /proc/cpuinfo')
processor : 0
vendor_id : AuthenticAMD
cpu family : 21
... ...
output # doesn't capture output
2. os.popen()
这个方法执行命令并返回执行后的信息对象,是通过一个管道文件将结果返回。
output = os.popen('cat /proc/cpuinfo')
output
print output.read()
processor : 0
vendor_id : AuthenticAMD
cpu family : 21
... ...
3. subprocess模块
该模块是一个功能强大的子进程管理模块,是替换os.system, os.spawn*等方法的一个模块。
import subprocess
subprocess.Popen(["ls", "-l"]) # python2.x doesn't capture
output
subprocess.run(["ls", "-l"]) # python3.x doesn't capture
output
total 68
drwxrwxr-x 3 xl xl 4096 Feb 8 05:00 com
drwxr-xr-x 2 xl xl 4096 Jan 21 02:58 Desktop
drwxr-xr-x 2 xl xl 4096 Jan 21 02:58 Documents
drwxr-xr-x 2 xl xl 4096 Jan 21 07:44 Downloads
... ...
相关问答
Q1: 如何在Java中调用Python代码
Jython(原JPython),是一个用Java语言写的Python解释器。 在没有第三方模块的情况下,通常选择利用Jython来调用Python代码, 它是一个开源的JAR包,你可以到官网下载 一个HelloPython程序 importorg.python.util.PythonInterpreter; publicclassHelloPython{ publicstaticvoidmain(String[]args){ PythonInterpreterinterpreter=newPythonInterpreter(); interpreter.exec("print('hello')"); } } 什么是PythonInterpreter?它的中文意思即是“Python解释器”。我们知道Python程序都是通过解释器来执行的,我们在Java中创建一个“解释器”对象,模拟Python解释器的行为,通过exec("Python语句")直接在JVM中执行Python代码,上面代码的输出结果为:hello在Jvm中执行Python脚本 interpreter.execfile("D:/labs/mytest/hello.py");如上,将exec改为execfile就可以了。需要注意的是,这个.py文件不能含有第三方模块,因为这个“Python脚本”最终还是在JVM环境下执行的,如果有第三方模块将会报错:javaImportError:Nomodulenamedxxx 仅在Java中调用Python编写的函数 先完成一个hello.py代码: defhello(): return'Hello'在Java代码中调用这个函数: importorg.python.core.PyFunction; importorg.python.core.PyObject; importorg.python.util.PythonInterpreter; publicclassHelloPython{ publicstaticvoidmain(String[]args){ PythonInterpreterinterpreter=newPythonInterpreter(); interpreter.execfile("D:/labs/hello.py"); PyFunctionpyFunction=interpreter.get("hello",PyFunction.class);//第一个参数为期望获得的函数(变量)的名字,第二个参数为期望返回的对象类型 PyObjectpyObject=pyFunction.__call__();//调用函数 System.out.println(pyObject); } } 上面的代码执行结果为:Hello 即便只是调用一个函数,也必须先加载这个.py文件,之后再通过Jython包中所定义的类获取、调用这个函数。 如果函数需要参数,在Java中必须先将参数转化为对应的“Python类型”,例如: __call__(newPyInteger(a),newPyInteger(b))a,b的类型为Java中的int型,还有诸如:PyString(Stringstring)、PyList(Iteratoriter)等。 详细可以参考官方的api文档。 包含第三方模块的情况:一个手写识别程序 这是我和舍友合作写的一个小程序,完整代码在这里:,界面上引用了corejava上的一段代码。Python代码是舍友写的,因为在Python程序中使用了第三方的NumPy模块,导致无法通过Jython执行。下面这个方法纯粹是个人思路,没有深入查资料。核心代码如下: importjava.io.*; classPyCaller{ privatestaticfinalStringDATA_SWAP="temp.txt"; privatestaticfinalStringPY_URL=System.getProperty("user.dir")+"\\test.py"; publicstaticvoidwriteImagePath(Stringpath){ PrintWriterpw=null; try{ pw=newPrintWriter(newFileWriter(newFile(DATA_SWAP))); }catch(IOExceptione){ e.printStackTrace(); } pw.print(path); pw.close(); } publicstaticStringreadAnswer(){ BufferedReaderbr; Stringanswer=null; try{ br=newBufferedReader(newFileReader(newFile(DATA_SWAP))); answer=br.readLine(); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } returnanswer; } publicstaticvoidexecPy(){ Processproc=null; try{ proc=Runtime.getRuntime().exec("python"+PY_URL); proc.waitFor(); }catch(IOExceptione){ e.printStackTrace(); }catch(InterruptedExceptione){ e.printStackTrace(); } } //测试码 publicstaticvoidmain(String[]args)throwsIOException,InterruptedException{ writeImagePath("D:\\labs\\mytest\\test.jpg"); execPy(); System.out.println(readAnswer()); } } 实际上就是通过Java执行一个命令行指令。
Q2: python内置函数
python内置函数是什么?一起来看下吧:
python内置函数有:
abs:求数值的绝对值
abs(-2) 2
pmod:返回两个数值的商和余数
pmod(5,2) (2,1) pmod(5.5,2) (2.0,1.5)
bool:根据传入的参数的逻辑值创建一个布尔值
bool() #未传入参数 False bool(0) #数值0、空序列等值为False False bool(1) True
all:判断可迭代对象的每个元素是否都为True值
all([1,2]) #列表中每个元素逻辑值均为True,返回True True all(()) #空元组 True all({}) #空字典 True
help:返回对象的帮助信息
help(str) Help on class str in module builtins: class str(object) | str(object='') - str | str(bytes_or_buffer[, encoding[, errors]]) - str | | Create a new string object from the given object. If encoding or | errors is specified, then the object must expose a data buffer | that will be decoded using the given encoding and error handler. | Otherwise, returns the result of object.__str__() (if defined) | or repr(object). | encoding defaults to sys.getdefaultencoding(). | errors defaults to 'strict'. | | Methods defined here: | | __add__(self, value, /) Return self+value.
_import_:动态导入模块
index = __import__('index') index.sayHello()
locals:返回当前作用域内的局部变量和其值组成的字典
def f(): print('before define a ') print(locals()) #作用域内无变量 a = 1 print('after define a') print(locals()) #作用域内有一个a变量,值为1 f f() before define a {} after define a {'a': 1}
input:读取用户输入值
s = input('please input your name:') please input your name:Ain s 'Ain'
open:使用指定的模式和编码打开文件,返回文件读写对象
# t为文本读写,b为二进制读写 a = open('test.txt','rt') a.read() 'some text' a.close()
eval:执行动态表达式求值
eval('1+2+3+4') 10
除了上述举例的函数之外,内置函数按分类还可分为:
1、数学运算(7个)
2、类型转换(24个)
3、序列操作(8个)
4、对象操作(7个)
5、反射操作(8个)
6、变量操作(2个)
7、交互操作(2个)
8、文件操作(1个)
9、编译操作(4个)
10、装饰器(3个)
Q3: java调python
很多朋友都想知道java怎么调python?下面就一起来了解一下吧~
java调python主要有两种方法:1.使用Runtime.getRuntime()执行脚本文件;2. 将python脚本写成进程为java提供服务,下面是具体的方法介绍:
第一种:使用Runtime.getRuntime()执行脚本文件
先建立python脚本文件 demo.py
import numpy as np a = np.arange(12).reshape(3,4)print(a)
java调用python程序并输出该结果
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Demo { public static void main(String[] args) { // TODO Auto-generated method stub Process proc; try { proc = Runtime.getRuntime().exec("python D:\\demo.py");// 执行py文件 //用输入输出流来截取结果 BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); proc.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }}
如若向python程序中函数传递参数并执行出结果,下面就举一例来说明一下。
同样建立python脚本文件demo2.py
import sys def func(a,b): return (a+b)if __name__ == '__main__': a = [] for i in range(1, len(sys.argv)): a.append((int(sys.argv[i]))) print(func(a[0],a[1]))
其中sys.argv用于获取参数url1,url2等。而sys.argv[0]代表python程序名,所以列表从1开始读取参数。
以上代码实现一个两个数做加法的程序,下面看看在java中怎么传递函数参数,代码如下:
int a = 18;int b = 23;try { String[] args = new String[] { "python", "D:\\demo2.py", String.valueOf(a), String.valueOf(b) }; Process proc = Runtime.getRuntime().exec(args);// 执行py文件 BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); proc.waitFor();} catch (IOException e) { e.printStackTrace();} catch (InterruptedException e) { e.printStackTrace();}
其中args是String[] { “python”,path,url1,url2 }; ,path是python程序所在的路径,url1是参数1,url2是参数2,以此类推。
2. 将python脚本写成进程为java提供服务
python脚本文件如下:
import socketimport sysimport threadingimport numpy as npfrom PIL import Imagedef main(): # 创建服务器套接字 serversocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 获取本地主机名称 host = socket.gethostname() # 设置一个端口 port = 12345 # 将套接字与本地主机和端口绑定 serversocket.bind((host,port)) # 设置监听最大连接数 serversocket.listen(5) # 获取本地服务器的连接信息 myaddr = serversocket.getsockname() print("服务器地址:%s"%str(myaddr)) # 循环等待接受客户端信息 while True: # 获取一个客户端连接 clientsocket,addr = serversocket.accept() print("连接地址:%s" % str(addr)) try: t = ServerThreading(clientsocket)#为每一个请求开启一个处理线程 t.start() pass except Exception as identifier: print(identifier) pass pass serversocket.close() passclass ServerThreading(threading.Thread): # words = text2vec.load_lexicon() def __init__(self,clientsocket,recvsize=1024*1024,encoding="utf-8"): threading.Thread.__init__(self) self._socket = clientsocket self._recvsize = recvsize self._encoding = encoding pass def run(self): print("开启线程.....") try: #接受数据 msg = '' while True: # 读取recvsize个字节 rec = self._socket.recv(self._recvsize) # 解码 msg += rec.decode(self._encoding) # 文本接受是否完毕,因为python socket不能自己判断接收数据是否完毕, # 所以需要自定义协议标志数据接受完毕 if msg.strip().endswith('over'): msg=msg[:-4] break sendmsg = Image.open(msg) # 发送数据 self._socket.send(("%s"%sendmsg).encode(self._encoding)) pass except Exception as identifier: self._socket.send("500".encode(self._encoding)) print(identifier) pass finally: self._socket.close() print("任务结束.....") pass def __del__(self): passif __name__ == "__main__": main()
在java代码中访问python进程的代码: package hello;import java.lang.System;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.InetAddress;import java.net.Socket;import java.io.OutputStream;import java.io.PrintStream;import java.io.InputStream;public class hello { public static void main(String[] args){ //System.out.println("Hello World!"); // TODO Auto-generated method stub try { InetAddress addr = InetAddress.getLocalHost(); String host=addr.getHostName(); //String ip=addr.getHostAddress().toString(); //获取本机ip //log.info("调用远程接口:host="+ip+",port="+12345); // 初始化套接字,设置访问服务的主机和进程端口号,HOST是访问python进程的主机名称,可以是IP地址或者域名,PORT是python进程绑定的端口号 Socket socket = new Socket(host,12345); // 获取输出流对象 OutputStream os = socket.getOutputStream(); PrintStream out = new PrintStream(os); // 发送内容 out.print( "F:\\xxx\\0000.jpg"); // 告诉服务进程,内容发送完毕,可以开始处理 out.print("over"); // 获取服务进程的输入流 InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8")); String tmp = null; StringBuilder sb = new StringBuilder(); // 读取内容 while((tmp=br.readLine())!=null) sb.append(tmp).append('\n'); System.out.print(sb); // 解析结果 //JSONArray res = JSON.parseArray(sb.toString()); } catch (IOException e) { e.printStackTrace(); }finally { try {if(socket!=null) socket.close();} catch (IOException e) {} System.out.print("远程接口调用结束."); } }}
Q4: python执行多进程时,如何获取函数返回的值
共享变量Python函数proc的方法。
没有办法直接实现Python函数proc你的需求Python函数proc,但是Python函数proc,你可以用共享变量的方法实现,比如Python函数proc:
def worker(procnum, return_dict):
'''worker function'''
print str(procnum) + ' represent!'
return_dict[procnum] = procnumif __name__ == '__main__':
manager = Manager()
return_dict = manager.dict()
jobs = [] for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,return_dict))
jobs.append(p)
p.start() for proc in jobs:
proc.join() print return_dict.values()
关于Python函数proc和python函数print的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








