python实现批处理文件

作者:fanyamin 时间:2022-08-14 19:27:46 

本文实例为大家分享了python实现批处理文件的具体代码,供大家参考,具体内容如下

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)

来源:https://blog.csdn.net/fanyamin/article/details/50792426

标签:python,批处理
0
投稿

猜你喜欢

  • Flash如何连接Mysql

    2010-11-11 11:57:00
  • Python日期的加减等操作的示例

    2021-10-06 14:28:14
  • 解决pytorch报错:AssertionError: Invalid device id的问题

    2021-05-15 16:13:42
  • SQL Server开发智能提示插件SQL Prompt介绍

    2024-01-23 01:02:54
  • php下载文件源代码(强制任意文件格式下载)

    2023-10-10 07:53:20
  • 简单了解什么是神经网络

    2023-10-11 22:26:34
  • Python实现树的先序、中序、后序排序算法示例

    2022-12-26 04:00:18
  • 用Oracle并行查询发挥多CPU的威力

    2010-07-23 12:52:00
  • PHP入门教程之面向对象的特性分析(继承,多态,接口,抽象类,抽象方法等)

    2023-11-18 08:17:16
  • 在Python中COM口的调用方法

    2023-10-05 14:16:39
  • 简单了解python中的与或非运算

    2021-03-20 01:45:02
  • pyramid配置session的方法教程

    2021-04-26 09:23:37
  • Mootools常用方法扩展(一)

    2009-01-09 12:45:00
  • 如何捕获人家站点的页面?

    2009-11-11 19:19:00
  • 使用coverage统计python web项目代码覆盖率的方法详解

    2023-05-23 18:24:35
  • 利用Python如何实时检测自身内存占用

    2023-01-11 03:12:02
  • Scrapy项目实战之爬取某社区用户详情

    2022-04-16 11:36:31
  • Python爬取阿拉丁统计信息过程图解

    2022-04-02 06:23:22
  • 关注oblog 关注xml-rpc 自己开发客户端小记

    2009-10-04 20:27:00
  • Pycharm2020最新激活码|永久激活(附最新激活码和插件的详细教程)

    2023-11-16 07:44:20
  • asp之家 网络编程 m.aspxhome.com