python版本的仿windows计划任务工具


Posted in Python onApril 30, 2018

计划任务工具-windows

计划任务工具根据自己设定的具体时间,频率,命令等属性来规定所要执行的计划。

效果图

python版本的仿windows计划任务工具

代码

# -*- coding: utf-8 -*-

"""
Module implementing App.
"""

from PyQt4.QtGui import QMainWindow
from PyQt4.QtCore import pyqtSignature
import time,os
import QtUtil
import shutil
import time

from v.Ui_App import Ui_MainWindow

class App(QMainWindow, Ui_MainWindow):
  """
  Class documentation goes here.
  """
  def __init__(self, parent = None):
    """
    Constructor
    """
    QMainWindow.__init__(self, parent)
    self.setupUi(self)
    
    # 创建'res/command'文件夹
    if os.path.exists('res/command'):
      pass
    else:
      os.mkdir('res/command')
    
#     self.startDate.textFromDateTime()
  
  
  @pyqtSignature("")
  def on_run_btn_clicked(self):
    """
            创建任务.
    """   
    
    # 在查询栏显示的内容
    cmd = self.taskRun.toPlainText()
    # 构建'res/command/01.txt'文件
    if not os.path.exists('res/command/01.txt'):
      m = open('res/command/01.txt','w')
      m.close()
    p = str(self.modifier.text())
    # 判断日期是否正确
    if self.endDate.text() <= self.startDate.text():
      QtUtil.showOkDialog(self, u'日期出错', u'结束日期要大于开始日期')
    
    elif not p.isdigit():
      QtUtil.showOkDialog(self, u'频率出错', u'运行频率必须为整数')
      
    else:
      # 如果任务条件不完整,则创建失败
      if self.taskName.text()=='' or self.modifier.text()=='' or cmd=='':
        QtUtil.showOkDialog(self, u'创建失败', u'任务内容缺失')
      else:
        # 读取'res/command'下所有文件的文件名到filename
        for root, dirs, files in os.walk('res/command'):
          for file in files:
            filename = open('res/filename.txt','a')
            filename.write('/')
  #           filename.truncate()
            filename.write(str(file))
            filename.close()
            filename1 = open('res/filename.txt')
            fn = filename1.read()
            filename1.close()
        print fn
        print '/' + str(self.taskName.text()) + '.cmd'
        # 如果任务名在filename中能找到,则说明任务已经存在
        if '/' + str(self.taskName.text()) + '.cmd' in fn:
          QtUtil.showOkDialog(self, u'创建失败', u'任务已存在')
           
        else:
          
          # 任务内容
          if self.schedule.currentText() == 'monthly':
            print 'monthly'
            run = 'schtasks /create /tn '+ self.taskName.text() + ' /tr '+os.getcwd()+'/res/command/'+self.taskName.text()+'.bat /sc ' + self.schedule.currentText() + ' /d ' +self.modifier.text() + ' /m ' + self.month.currentText() + ' /st ' + self.timeEdit.text() + ' /sd ' + self.startDate.text() + ' /ed ' + self.endDate.text()
          elif self.schedule.currentText() == 'once':
            print 'once'
            run = 'schtasks /create /tn '+ self.taskName.text() + ' /tr '+os.getcwd()+'/res/command/'+self.taskName.text()+'.bat /sc ' + self.schedule.currentText() + ' /st ' + self.timeEdit.text() + ' /sd ' + self.startDate.text() 
            if self.startDate.text() < time.strftime('%Y/%m/%d') or (self.timeEdit.text() <= time.strftime('%H:%M:%S') and self.startDate.text() == time.strftime('%Y/%m/%d')) :
              QtUtil.showOkDialog(self, u'时间错误', u'设置时间早于当前时间')
              return
          else: 
            print 'not monthly'
            run = 'schtasks /create /tn '+ self.taskName.text() + ' /tr '+os.getcwd()+'/res/command/'+self.taskName.text()+'.bat /sc ' + self.schedule.currentText() + ' /mo ' +self.modifier.text() + ' /st ' + self.timeEdit.text() + ' /sd ' + self.startDate.text() + ' /ed ' + self.endDate.text()
          
          # 创建命令文件
          fd = open('res/command/'+self.taskName.text()+'.bat','w')
          fd.write(cmd)
          fd.close()
          
          # 创建任务文件
          f = open('res/command/'+self.taskName.text()+'.cmd','w')
          f.write(run)
          f.close()
          # 创建任务
          os.system(os.getcwd()+'\\res\command\\'+str(self.taskName.text())+'.cmd')
          QtUtil.showOkDialog(self, u'创建成功', u'创建成功')
  @pyqtSignature("")
  def on_delete_btn_clicked(self):
    """
            删除任务.
    """
    # 强制删除删除框内任务
    x=os.system('schtasks /delete /tn '+str(self.taskDelete.text()).decode('gbk')+' /f')
    # 如果已经任务已经删除,则报任务不存在
    if x==1:
      QtUtil.showOkDialog(self, u'删除失败', u'任务名错误或不存在该任务')
    else:
      os.remove('res/filename.txt')
      
      
      if os.path.exists('res/command/'+self.taskDelete.text()+'.cmd'):
        os.remove('res/command/'+str(self.taskDelete.text())+'.bat')
        os.remove('res/command/'+str(self.taskDelete.text())+'.cmd')
        
      # 读取'res/command'下所有文件的文件名到filename
      for root, dirs, files in os.walk('res/command'):
        for file in files:
          filename = open('res/filename.txt','a')
          filename.write('/')
#           filename.truncate()
          filename.write(str(file))
          filename.close()
          filename1 = open('res/filename.txt')
          fn = filename1.read()
          filename1.close()
      # 删除任务,并删除命令文件与任务文件
      QtUtil.showOkDialog(self, u'删除成功', u'删除成功') 
      
  @pyqtSignature("")
  def on_query_btn_clicked(self):
    """
            查询任务.
    """
    # 调整 936 为 437 美国编码,才可运行
    os.system('chcp 437')
    # 查询任务
    os.system('schtasks /query')
    # 在生成新log文件前先删除以前的log文件
    if os.path.exists('res/log.txt'):
      os.remove('res/log.txt')
    
    # 遍历'res/command'的所有文件,将所有文件内容复制到log文件中
    for root, dirs, files in os.walk('res/command'):
      for file in files:
        dir = str(root)+'/'+str(file)
        f = open(dir,'r')
        scripts = f.read()
        new_path_filename = 'res/log.txt'
        
        f = open(new_path_filename, 'a')
        f.write(scripts) 
        f.write('\n')
        f.close()
    # 读取log文件
    if os.path.exists('res/log.txt'):
      fd=open('res/log.txt')
      info = fd.read()
      fd.close()
      # 在查询窗口显示log文件内容
      self.taskQuery.setText(str(info))
    else:
      QtUtil.showOkDialog(self, u'失败', u'不存在任务')
  
  @pyqtSignature("")
  def on_delall_btn_clicked(self):
    """
            清空任务.
    """
    os.system('schtasks /delete /tn * /f')
    if os.path.exists('res/log.txt'):
      os.remove('res/log.txt')
    if os.path.exists('res/filename.txt'):  
      os.remove('res/filename.txt')
    shutil.rmtree('res/command')
    os.mkdir('res/command')
    QtUtil.showOkDialog(self, u'成功', u'任务清空')

“任务名称”填写任务的名字,计划类型选择时间,频率填写次数,在计划类型中除了monthly之外的其他类型都填写频率,monthly时日期填写日期号数,月份也只在选择monthly时候需要选择,其他时候不用选择,月份中*号问任意月,接着填写开始时间、开始日期、结束日期,结束日期要大于开始日期,最后填写所要执行的命令,则任务创建成功。,创建任务后随时可以查阅任务,点击查询任务即可,删除任务只要填上要删除的任务名称,点击删除任务即可,清空任务为删除所有任务。

本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )

Python 相关文章推荐
python实现异步回调机制代码分享
Jan 10 Python
Python中的urllib模块使用详解
Jul 07 Python
Python中Django发送带图片和附件的邮件
Mar 31 Python
python3之微信文章爬虫实例讲解
Jul 12 Python
如何在sae中设置django,让sae的工作环境跟本地python环境一致
Nov 21 Python
python的socket编程入门
Jan 29 Python
Python中super函数用法实例分析
Mar 18 Python
python selenium实现发送带附件的邮件代码实例
Dec 10 Python
python 控制台单行刷新,多行刷新实例
Feb 19 Python
python 实现PIL模块在图片画线写字
May 16 Python
使用gunicorn部署django项目的问题
Dec 30 Python
OpenCV-Python实现油画效果的实例
Jun 08 Python
Python装饰器原理与用法分析
Apr 30 #Python
Python中pillow知识点学习
Apr 30 #Python
Python生成器定义与简单用法实例分析
Apr 30 #Python
Python迭代器定义与简单用法分析
Apr 30 #Python
python 实现在txt指定行追加文本的方法
Apr 29 #Python
Python 实现在文件中的每一行添加一个逗号
Apr 29 #Python
python 把文件中的每一行以数组的元素放入数组中的方法
Apr 29 #Python
You might like
用PHP和ACCESS写聊天室(六)
2006/10/09 PHP
比file_get_contents稳定的curl_get_contents分享
2012/01/11 PHP
php建立Ftp连接的方法
2015/03/07 PHP
简单的php购物车代码
2020/06/05 PHP
如何让动态插入的javascript脚本代码跑起来。
2007/01/09 Javascript
ExtJS 工具栏 分页事件参数
2010/03/05 Javascript
jQuery下的几个你可能没用过的功能
2010/08/29 Javascript
关于jQuery对象数据缓存Cache原理以及jQuery.data详解
2013/04/07 Javascript
javascript break指定标签打破多层循环示例
2014/01/20 Javascript
node.js中的fs.fchmod方法使用说明
2014/12/16 Javascript
jquery 重写 ajax提交并判断权限后 使用load方法报错解决方法
2016/01/19 Javascript
JS实现关闭当前页而不弹出提示框的方法
2016/06/22 Javascript
web前端开发upload上传头像js示例代码
2016/10/22 Javascript
ES6中的箭头函数实例详解
2017/04/06 Javascript
AngularJS获取json数据的方法详解
2017/05/27 Javascript
angularjs定时任务的设置与清除示例
2017/06/02 Javascript
深入了解js原型模式
2019/05/30 Javascript
JS数组push、unshift、pop、shift方法的实现与使用方法示例
2020/04/29 Javascript
python进阶教程之模块(module)介绍
2014/08/30 Python
python服务器与android客户端socket通信实例
2014/11/12 Python
浅谈Python中的闭包
2015/07/08 Python
Python中的连接符(+、+=)示例详解
2017/01/13 Python
Python基于列表模拟堆栈和队列功能示例
2018/01/05 Python
Python使用pickle模块报错EOFError Ran out of input的解决方法
2018/08/16 Python
python通过paramiko复制远程文件及文件目录到本地
2019/04/30 Python
python字符串Intern机制详解
2019/07/01 Python
使用python将最新的测试报告以附件的形式发到指定邮箱
2019/09/20 Python
浅析python标准库中的glob
2020/03/13 Python
原生canvas制作画图小工具的踩坑和爬坑
2020/06/09 HTML / CSS
来自世界上最好大学的在线课程:edX
2018/10/16 全球购物
英国外籍人士的在线超市:British Corner Shop
2019/06/03 全球购物
音乐器材管理制度
2014/01/31 职场文书
年度考核自我鉴定
2014/02/02 职场文书
优秀英文求职信范文
2015/03/19 职场文书
地心历险记观后感
2015/06/15 职场文书
PyQt5实现多张图片显示并滚动
2021/06/11 Python