python实现批处理

  • 1.为什么需要批处理
  • 2.subprocess库中常用的封装函数
  • subprocess.call()
  • subprocess.run()
  • subprocess.Popen()
  • 3.一个完整的python项目实现AVS_PCC的批处理



一般提到写批处理大家都会想到windows下的bat以及linux下的shell等实现批处理文件的编写。但是bat和shell命令太复杂,可以使用python替代bat写批处理。

1.为什么需要批处理

假设你有一个exe文件需要反复运行,那么你怎样才可以做到像c++等编程语言对一个变量进行不断自加一样简单实现反复运行呢,这时候你就要用到批处理了。

2.subprocess库中常用的封装函数

Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.systemos.spawnos.popenpopen2.commands.不但可以调用外部的命令作为子进程,而且可以连接到子进程的input/output/error管道,获取相关的返回信息。
注:以下代码均在window下的python3.7.4版本下实现,如果出现缺少库的情况,请使用 pip install XX 安装需要的库。

subprocess.call()

父进程等待子进程完成,返回退出信息

#父进程等待子进程完成
subprocess.call   	#call 系列 都是等待命令执行完
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None)
#参数含义
args    #该参数用于启动进程。这可能是一个列表或一个字符串。
returncode   #该参数表示子进程的退出状态。通常情况下,0作为退出状态表明它成功运行。负值-N表明子进程被信号N终止(仅POSIX)。
stdout  #该参数表示标准输出
stderr	#该参数表示标准错误

一个例子教你怎么使用call函数

subprocess.call("encode.exe",stdout=open('out.txt', 'a'), stderr=open('err.txt', 'a'))

其中encode.exe是你要运行的可执行文件,out.txt是你存放输出信息的位置,err.txt是你存放错误信息的位置。

subprocess.run()

父进程等待子进程完成

#父进程等待子进程完成
subprocess.call		#执行指定的命令,等待命令执行完成后返回一个包含执行结果的CompletedProcess类的实例。
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False)
#参数含义
args    #该参数用于启动进程。这可能是一个列表或一个字符串。
returncode   #该参数表示子进程的退出状态。通常情况下,0作为退出状态表明它成功运行。负值-N表明子进程被信号N终止(仅POSIX)。
stdout  #该参数表示标准输出
stderr	#该参数表示标准错误

subprocess.Popen()

class Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
#参数含义
args    #该参数用于启动进程。这可能是一个列表或一个字符串。
returncode   #该参数表示子进程的退出状态。通常情况下,0作为退出状态表明它成功运行。负值-N表明子进程被信号N终止(仅POSIX)。
stdout  #该参数表示标准输出
stderr	#该参数表示标准错误

一个例子见博客。

3.一个完整的python项目实现AVS_PCC的批处理

下面的python代码可以对AVS_PCC的Cat1序列进行批处理,将输出的编解码信息以及失真信息按文件名输出到txt文档中。里面有详细的解释,基本上看注释就能看懂每条语句的用处,如果看代码还有不懂的地方可以在下面留言,博主看到后将尽快回复。

import os,sys,subprocess
plyPath=r'E:\\AVS\\AVS_data\\hd_map_1mm\\'
plyName=['bridge_1mm','double_T_section_1mm','intersection1_1mm','intersection2_1mm','straight_road_1mm','T_section_1mm']
#EXE=['0','1']
EXE=['0']
CTC=['C1','C2','C3','C4']
curPath = os.getcwd()#获取当前路径
# print(curPath)
for ctc in CTC:
	if ctc == 'C1':
		#下面是cat1的C1部分
		QSQPC1=[['512','48'],['256','40'],['64','32'],['32','24'],['8','16'],['4','8']]#这里是几何步长和属性步长组成的二维数组
		for ply_name in plyName:
			for e in EXE:
				fileName = curPath+'\\C1_'+ply_name+'_'+e+'.txt'
				#删除已经存在的文件
				if os.path.exists(fileName): 
					os.remove(fileName)#删除已经存在的文件
				for	qsqp in QSQPC1:		
					command1=e+'_encoder.exe'+' -c encode.cfg --input '+plyPath+ply_name+'.ply -ctf 1 -grdf 1 --geom_quant_step '+qsqp[0]+' --attr_quant_param '+qsqp[1]
					subprocess.call(command1,stdout=open('C1_'+ply_name+'_'+e+'.txt', 'a'), stderr=open('err.txt', 'a'))
	elif ctc == 'C2':			
		#下面是cat1的C2部分	
		Quant_stepC2=['48','40','32','24','16','8']	
		for ply_name in plyName:
			for qs in Quant_stepC2:
				for e in EXE:
					fileName = curPath+'\\C2_'+ply_name+'_'+e+'.txt'
					#删除已经存在的文件
					if os.path.exists(fileName): 
						os.remove(fileName) 
					command1=e+'_encoder.exe'+' -c encode.cfg --input '+plyPath+ply_name+'.ply -ctf 1 --attr_quant_param '+qs
					subprocess.call(command1,stdout=open('C2_'+ply_name+'_'+e+'.txt', 'a'), stderr=open('err.txt', 'a'))
	elif ctc == 'C3':					
		#下面是cat1的C3部分
		Quant_stepC3=['40','32','24','16','8']
		for ply_name in plyName:
			for qs in Quant_stepC3:
				for e in EXE:
					fileName = curPath+'\\C3_'+ply_name+'_'+e+'.txt'
					#删除已经存在的文件
					if os.path.exists(fileName): 
						os.remove(fileName) 
					command1=e+'_encoder.exe'+' -c encode.cfg --input '+plyPath+ply_name+'.ply --attr_quant_param '+qs
					subprocess.call(command1,stdout=open('C3_'+ply_name+'_'+e+'.txt', 'a'), stderr=open('err.txt', 'a'))
	elif ctc == 'C4':		
		#下面是cat1的C4部分
		for ply_name in plyName:
			for e in EXE:
				fileName = curPath+'\\C4_'+ply_name+'_'+e+'.txt'
				#删除已经存在的文件
				if os.path.exists(fileName): 
					os.remove(fileName) 
				command1=e+'_encoder.exe'+' -c encode.cfg --input '+plyPath+ply_name+'.ply'
				subprocess.call(command1,stdout=open('C4_'+ply_name+'_'+e+'.txt', 'a'), stderr=open('err.txt', 'a'))