python实现批处理文件

 更新时间:2020年7月28日 15:23  点击:2218

Windows下的bat, linux 下的shell 用来做批处理都很好用,可惜不通用
用 Python 来做就简单多了,不过一条条写代码来调用系统命令也够烦的了
程序员都很懒, 不愿做机械无谓的重复性工作, 干脆自己实现一个.

用法超级简单, 默认会执行一个自定义的 batch.json, 按顺序一条条执行其中的步骤

{"steps": 
  [
  {"step":"df -h","desc":"display disk space usage"},
  {"step":"date","desc":"display the current dater"},
  {"step":"time","desc":"display the current time"}
  ]
}

用法:

python batch.py

当然也可以指定不同的步骤文件 , 例如

python batch.py xxx.json

运行结果以markdown形式输出, 例如

$ python batch.py
Usage: python batch.py <batch_json_file>
note: execute the batch.json by default
# Execute batch.json begin
---------------------------

## Will execute 3 steps
~~~~~~~~~~~~~~~~~~~~~~~~~~~
0. [df -h]: display disk space usage
1. [date]: display the current dater
2. [time]: display the current time

* 0. [df -h]: display disk space usage
Filesystem             Size  Used Avail Capacity iused  ifree %iused Mounted on
/dev/disk1             233Gi 208Gi  24Gi  90% 54622825 6364694  90%  /
devfs               329Ki 329Ki  0Bi  100%   

* 1. [date]: display the current dater
Thu Mar 3 22:50:21 CST 2016

* 2. [time]: display the current time

real  0m0.001s
user  0m0.000s
sys 0m0.000s

## Done the following steps
~~~~~~~~~~~~~~~~~~~~~~~~~~~
0. [df -h]: display disk space usage
1. [date]: display the current dater
# Execute batch.json end.

Python源代码如下, 希望有人能用得上

'''
  like bat file, execute the steps in batch.json 
'''
import os,sys,subprocess
import time,thread
import codecs
import json
from datetime import datetime
from subprocess import call
from pprint import pprint

def execute_json(json_file):
  print "# Execute {0} begin\n---------------------------".format(json_file)

  json_data=open(json_file)
  data = json.load(json_data)
  cnt = len(data['steps'])
  i = 0
  print "\n## Will execute {0} steps \n~~~~~~~~~~~~~~~~~~~~~~~~~~~".format(cnt)
  for i in range(0, cnt):
      print "{0}. [{1}]: {2}".format(i, data['steps'][i]['step'], data['steps'][i]['desc'])

  #pprint(data)
  #print("cnt=", cnt)
  for i in range(0, cnt):
    cmd = data['steps'][i]['step']
    desc = data['steps'][i]['desc']
    print "\n* {0}. [{1}]: {2} ".format(i, cmd, desc)

    if(cmd.startswith('cd')):
      cmd = cmd.replace("cd ", "")
      os.chdir(cmd)
    else:
      ret = os.system(cmd)
      if(ret != 0):
        print "Encounter error of step {0}. {1}, error code={2}".format(i, cmd, ret)
        break

  print "\n## Done the following steps\n~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  for j in range(0, i):
    print "{0}. [{1}]: {2}".format(j, data['steps'][j]['step'], data['steps'][j]['desc'])  
  json_data.close()
  print "# Execute {0} end.".format(json_file)

if __name__ == "__main__":
  argc = len(sys.argv)
  step_file = 'batch.json'
  if( argc > 1):
    idx = 1
    while(idx < argc):
      step_file = sys.argv[idx]
      execute_json(step_file)
      idx = idx + 1
  else:
    print "Usage: python {0} <batch_json_file>".format(sys.argv[0])
    print "note: execute the batch.json by default"
    execute_json(step_file)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • python opencv 画外接矩形框的完整代码

    这篇文章主要介绍了python-opencv-画外接矩形框的实例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-04
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • 最炫Python烟花代码全解析

    2022虎年新年即将来临,小编为大家带来了一个利用Python编写的虎年烟花特效,堪称全网最绚烂,文中的示例代码简洁易懂,感兴趣的同学可以动手试一试...2022-02-14
  • python中numpy.empty()函数实例讲解

    在本篇文章里小编给大家分享的是一篇关于python中numpy.empty()函数实例讲解内容,对此有兴趣的朋友们可以学习下。...2021-02-06
  • python-for x in range的用法(注意要点、细节)

    这篇文章主要介绍了python-for x in range的用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-10
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • python实现b站直播自动发送弹幕功能

    这篇文章主要介绍了python如何实现b站直播自动发送弹幕,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下...2021-02-20
  • python Matplotlib基础--如何添加文本和标注

    这篇文章主要介绍了python Matplotlib基础--如何添加文本和标注,帮助大家更好的利用Matplotlib绘制图表,感兴趣的朋友可以了解下...2021-01-26
  • 解决python 使用openpyxl读写大文件的坑

    这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
  • Windows批量搜索并复制/剪切文件的批处理程序实例

    这篇文章主要介绍了Windows批量搜索并复制/剪切文件的批处理程序实例,需要的朋友可以参考下...2020-06-30
  • python 计算方位角实例(根据两点的坐标计算)

    今天小编就为大家分享一篇python 计算方位角实例(根据两点的坐标计算),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
  • python实现双色球随机选号

    这篇文章主要为大家详细介绍了python实现双色球随机选号,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-05-02
  • python中使用np.delete()的实例方法

    在本篇文章里小编给大家整理的是一篇关于python中使用np.delete()的实例方法,对此有兴趣的朋友们可以学习参考下。...2021-02-01
  • 使用Python的pencolor函数实现渐变色功能

    这篇文章主要介绍了使用Python的pencolor函数实现渐变色功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-09
  • BAT批处理判断服务是否正常运行的方法(批处理命令综合应用)

    批处理就是对某对象进行批量的处理,通常被认为是一种简化的脚本语言,它应用于DOS和Windows系统中。这篇文章主要介绍了BAT批处理判断服务是否正常运行(批处理命令综合应用),需要的朋友可以参考下...2020-06-30
  • python自动化办公操作PPT的实现

    这篇文章主要介绍了python自动化办公操作PPT的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-05
  • Python getsizeof()和getsize()区分详解

    这篇文章主要介绍了Python getsizeof()和getsize()区分详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-20
  • 解决python 两个时间戳相减出现结果错误的问题

    这篇文章主要介绍了解决python 两个时间戳相减出现结果错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-12
  • python实现学生通讯录管理系统

    这篇文章主要为大家详细介绍了python实现学生通讯录管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-25