首页 > 技术文章 > python:执行一个命令行N次

hydonlee 2016-05-09 17:13 原文

经常希望可以执行一个命令行N次。。。windows下没有现成的工具(有?推荐给我!)

用python写一个。。。

#!/usr/bin/evn python
#coding: utf-8

"""
times.py

run a command line for n times
"""
import os 
import sys
import string

if __name__ == "__main__":
    n = 1
    cmd = ""
    
    if len(sys.argv) >= 3: 
        n = int(sys.argv[1])
        cmd = string.join(sys.argv[2:], ' ')
    else:
        print '''error command line

Useage:
    times n "command line"
'''
        exit(1)
    
    for x in range(n):
        os.system(cmd)

实现效果

C:\Users\hydon>times 10 echo hello world.
hello world.
hello world.
hello world.
hello world.
hello world.
hello world.
hello world.
hello world.
hello world.
hello world.

推荐阅读