python:執行一個命令行N次


經常希望可以執行一個命令行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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM