python实现批处理文件
作者:fanyamin 发布时间:2022-08-14 19:27:46
标签:python,批处理
本文实例为大家分享了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


猜你喜欢
- Bootstrap简介Bootstrap,来自 Twitter,是目前最受欢迎的前端框架。Bootstrap 是基于 HTML、CSS、JA
- 废话不多说,直接上代码/** * lhgcalendar时间插件限制只能选择三个月 * @d 获取到的开始时间 * @m 要限制的时间的长度
- 前言最近开始学习python数据库编程后,在了解了基本概念,打算上手试验一下时,卡在了MYSQLdb包的安装上,折腾了半天才解决。记录一下我
- 在没学习开窗函数之前,我们都知道,用了分组之后,查询字段就只能是分组字段和聚合的字段,这带来了极大的不方便,有时我们查询时需要分
- 本文实例讲述了JS简单模拟触发按钮点击功能的方法。分享给大家供大家参考,具体如下:<html> <head&g
- 1. 开发1.1. 架构Gorm使用可链接的API,*gorm.DB是链的桥梁,对于每个链API,它将创建一个新的关系。db, err :=
- 当鼠标滑过一个图片时,图片会变成另外一张图片的效果是怎么制作出来的呢?相对一些不熟悉HTML代码的朋友来说,可以使用网页编辑软件自带的动作来
- 最近网上再度兴起了CSS布局和Table 布局的争论。我最初颇有些不以为然:我原以为CSS 布局的意义早已深入人心,却没想到还有这么多设计师
- 本文实例讲解了PHP图片上传并压缩的实现方法,分享给大家供大家参考,具体内容如下使用到三个文件connect.php:连接数据库test_u
- 学习前言看了好多Github,用于保存模型的库都是Keras,我觉得还是好好学习一下的好什么是KerasKeras是一个由Python编写的
- python异步IO初探探索异步IO执之前,先说说IO的种类1.阻塞IO最简单,即读写数据时,需要等待操作完成,才能继续执行。进阶的做法就是
- 这一款是用原生javascript实现的分页插件pagenav,页码显示jquery插件,只需要存在#pageNav,则会在其中显示页码,调
- 从本文开始,本系列将介绍python简单案例并进行代码展示,本文的案例是利用pandas库实现读取csv文件并按照列的从小到大进行排序。前言
- 1.基数排序基数排序的基本思想是先将数字按照个位数上数字的大小进行排序,排序之后再将已经排过序的数字再按照十位数上数字的大小进行排序,依次推
- 函数画图以 z = x 2 + y 2 为例#导入模块import numpy as npimport matplotlib.py
- Python操作MySQL主要使用两种方式:原生模块 pymsqlORM框架 SQLAchemypymqlpymsql是Python中操作M
- 近来实验室的师姐要 * 文,由于论文交稿时间临近,有一些杂活儿需要处理,作为实验室资历最浅的一批,我这个实习生也就责无旁贷地帮忙当个下手。今天
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 14 - Periodical and Intro
- 例子:http.Handle("/tmpfiles/", http.StripPrefix("/tmpfile
- 1、没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2、I/O吞吐量小,形成了瓶颈效应。 3、没有 创建计算列导致查询