python版本的仿windows計(jì)劃任務(wù)工具
計(jì)劃任務(wù)工具-windows
計(jì)劃任務(wù)工具根據(jù)自己設(shè)定的具體時(shí)間,頻率,命令等屬性來(lái)規(guī)定所要執(zhí)行的計(jì)劃。
效果圖

代碼
# -*- 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)
# 創(chuàng)建'res/command'文件夾
if os.path.exists('res/command'):
pass
else:
os.mkdir('res/command')
# self.startDate.textFromDateTime()
@pyqtSignature("")
def on_run_btn_clicked(self):
"""
創(chuàng)建任務(wù).
"""
# 在查詢欄顯示的內(nèi)容
cmd = self.taskRun.toPlainText()
# 構(gòu)建'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'日期出錯(cuò)', u'結(jié)束日期要大于開(kāi)始日期')
elif not p.isdigit():
QtUtil.showOkDialog(self, u'頻率出錯(cuò)', u'運(yùn)行頻率必須為整數(shù)')
else:
# 如果任務(wù)條件不完整,則創(chuàng)建失敗
if self.taskName.text()=='' or self.modifier.text()=='' or cmd=='':
QtUtil.showOkDialog(self, u'創(chuàng)建失敗', u'任務(wù)內(nèi)容缺失')
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'
# 如果任務(wù)名在filename中能找到,則說(shuō)明任務(wù)已經(jīng)存在
if '/' + str(self.taskName.text()) + '.cmd' in fn:
QtUtil.showOkDialog(self, u'創(chuàng)建失敗', u'任務(wù)已存在')
else:
# 任務(wù)內(nèi)容
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'時(shí)間錯(cuò)誤', u'設(shè)置時(shí)間早于當(dāng)前時(shí)間')
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()
# 創(chuàng)建命令文件
fd = open('res/command/'+self.taskName.text()+'.bat','w')
fd.write(cmd)
fd.close()
# 創(chuàng)建任務(wù)文件
f = open('res/command/'+self.taskName.text()+'.cmd','w')
f.write(run)
f.close()
# 創(chuàng)建任務(wù)
os.system(os.getcwd()+'\\res\command\\'+str(self.taskName.text())+'.cmd')
QtUtil.showOkDialog(self, u'創(chuàng)建成功', u'創(chuàng)建成功')
@pyqtSignature("")
def on_delete_btn_clicked(self):
"""
刪除任務(wù).
"""
# 強(qiáng)制刪除刪除框內(nèi)任務(wù)
x=os.system('schtasks /delete /tn '+str(self.taskDelete.text()).decode('gbk')+' /f')
# 如果已經(jīng)任務(wù)已經(jīng)刪除,則報(bào)任務(wù)不存在
if x==1:
QtUtil.showOkDialog(self, u'刪除失敗', u'任務(wù)名錯(cuò)誤或不存在該任務(wù)')
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()
# 刪除任務(wù),并刪除命令文件與任務(wù)文件
QtUtil.showOkDialog(self, u'刪除成功', u'刪除成功')
@pyqtSignature("")
def on_query_btn_clicked(self):
"""
查詢?nèi)蝿?wù).
"""
# 調(diào)整 936 為 437 美國(guó)編碼,才可運(yùn)行
os.system('chcp 437')
# 查詢?nèi)蝿?wù)
os.system('schtasks /query')
# 在生成新log文件前先刪除以前的log文件
if os.path.exists('res/log.txt'):
os.remove('res/log.txt')
# 遍歷'res/command'的所有文件,將所有文件內(nèi)容復(fù)制到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文件內(nèi)容
self.taskQuery.setText(str(info))
else:
QtUtil.showOkDialog(self, u'失敗', u'不存在任務(wù)')
@pyqtSignature("")
def on_delall_btn_clicked(self):
"""
清空任務(wù).
"""
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'任務(wù)清空')
“任務(wù)名稱”填寫(xiě)任務(wù)的名字,計(jì)劃類(lèi)型選擇時(shí)間,頻率填寫(xiě)次數(shù),在計(jì)劃類(lèi)型中除了monthly之外的其他類(lèi)型都填寫(xiě)頻率,monthly時(shí)日期填寫(xiě)日期號(hào)數(shù),月份也只在選擇monthly時(shí)候需要選擇,其他時(shí)候不用選擇,月份中*號(hào)問(wèn)任意月,接著填寫(xiě)開(kāi)始時(shí)間、開(kāi)始日期、結(jié)束日期,結(jié)束日期要大于開(kāi)始日期,最后填寫(xiě)所要執(zhí)行的命令,則任務(wù)創(chuàng)建成功。,創(chuàng)建任務(wù)后隨時(shí)可以查閱任務(wù),點(diǎn)擊查詢?nèi)蝿?wù)即可,刪除任務(wù)只要填上要?jiǎng)h除的任務(wù)名稱,點(diǎn)擊刪除任務(wù)即可,清空任務(wù)為刪除所有任務(wù)。
本站文章為 寶寶巴士 SD.Team 原創(chuàng),轉(zhuǎn)載務(wù)必在明顯處注明:(作者官方網(wǎng)站: 寶寶巴士 )
相關(guān)文章
python實(shí)現(xiàn)對(duì)服務(wù)器腳本敏感信息的加密解密功能
這篇文章主要介紹了python實(shí)現(xiàn)對(duì)服務(wù)器腳本敏感信息的加密解密功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Python?socket之TCP通信及下載文件的實(shí)現(xiàn)
本文主要介紹了Python?socket之TCP通信及下載文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
python Tornado事件循環(huán)示例源碼解析
這篇文章主要為大家介紹了python Tornado事件循環(huán)示例源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Django的HttpRequest和HttpResponse對(duì)象詳解
這篇文章主要介紹了Django的HttpRequest和HttpResponse對(duì)象,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
python base64庫(kù)給用戶名或密碼加密的流程
這篇文章主要介紹了python base64庫(kù)給用戶名或密碼加密,需要的朋友可以參考下2020-01-01

