2011-08-12 125 views
1

我試圖在用戶給定的某個時間在Python腳本中運行一個函數。爲此,我使用日期時間模塊。輸入時間並將其與用戶輸入進行比較

這也是迄今爲止的部分代碼:

import os 
import subprocess 
import shutil 
import datetime 
import time 

def process(): 

    path = os.getcwd() 
    outdir = os.getcwd() + '\Output' 

    if not os.path.exists(outdir): 
     os.mkdir(outdir, 0777) 

    for (root, dirs, files) in os.walk(path): 
     filesArr = [] 
     dirname = os.path.basename(root) 
     parent_dir = os.path.basename(path) 

     if parent_dir == dirname: 
      outfile = os.path.join(outdir, ' ' + dirname + '.pdf') 
     else: 
      outfile = os.path.join(outdir, parent_dir + ' ' + dirname + '.pdf') 

     print " " 
     print 'Processing: ' + path 

     for filename in files: 
      if root == outdir: 
       continue 
      if filename.endswith('.pdf'): 
       full_name = os.path.join(root, filename) 
       if full_name != outfile: 
        filesArr.append('"' + full_name + '"') 

     if filesArr: 
      cmd = 'pdftk ' + ' '.join(filesArr) + ' cat output "' + outfile + '"' 
      print " " 
      print 'Merging: ' + str(filesArr) 

      print " " 

      sp = subprocess.Popen(cmd) 

      print "Finished merging documents successfully." 

      sp.wait() 

    return 

now = datetime.datetime.now() 
hour = str(now.hour) 
minute = str(now.minute) 
seconds = str(now.second) 
time_1 = hour + ":" + minute + ":" + seconds 

print "Current time is: " + time_1 

while True: 
    time_input = raw_input("Please enter the time in HH:MM:SS format: ") 

    try: 
     selected_time = time.strptime(time_input, "%H:%M:%S") 
     print "Time selected: " + str(selected_time) 

     while True: 
      if (selected_time == time.localtime()): 
      print "Beginning merging process..." 
      process() 
      break 
      time.sleep(5) 

     break 

    except ValueError: 
     print "The time you entered is incorrect. Try again." 

的問題是具有正試圖找到如何與當前時間比較用戶輸入的時間的方式(如,當前時間當腳本運行時)。另外,如何在給定的時間運行並處理一個函數?

回答

0

我可以看到各種各樣的事情在你提出代碼註釋,但最主要的是在selected_time = selected_hour + ...,因爲我覺得你與不同的單位增加的整數。你應該從selected_time = selected_hour * 3600 + ...開始。

第二個問題是當您嘗試檢查輸入的有效性時:由於用戶沒有被要求輸入另一個值,所以您在無法發展的支票上設置while。這意味着這些循環將永遠不會結束。

然後,關於魯棒性的一些事情:或許您應該將選定時間與當前時間進行比較,採用更靈活的方式,即將==替換爲>=或某些增量。

最後一件事,就可以使Python腳本等待用下面的命令:

import time 
time.sleep(some_duration) 

其中some_duration是float,在幾秒鐘的意思。

請問您能檢查一下現在是否有效嗎?

+0

我更新了代碼並按照你的說法做了,並且還使用了@alexandru Plugaru的代碼建議,但是它沒有正確輸出時間()沒有運行 – Brian

+0

你在比較時忽略了比較變化,時間「和」selected_time「接近結束。你必須做的是與delta的比較:'如果selected_time> = current_time> = selected_time + delta',否則你可能會在你的預定時間周圍。第二個說法,「時間」是模塊,而不是當前時間,所以你的比較將無法工作! –

+0

@Brian,我在這裏回答,因爲我無法回答你對亞歷山德魯的問題。你所遇到的問題是,你期望'selected_time'和當前時間相等。這種情況不太可能發生,因爲您的等待時間爲5秒。在你的比較中,你應該在'expected_time'周圍留出一些空白(見上面我的評論)。 –

0

首先,我建議你看看:http://docs.python.org/library/time.html#time.strptime 這可能會證明在你試圖驗證時間時有幫助。

可以是這樣的: 進口時間

import time 

while True: #Infinite loop   
    time_input = raw_input("Please enter the time in HH:MM:SS format: ") 
    try: 
     current_date = time.strftime("%Y %m %d") 
     my_time = time.strptime("%s %s" % (current_date, time_input), 
          "%Y %m %d %H:%M:%S") 
     break #this will stop the loop 
    except ValueError: 
     print "The time you entered is incorrect. Try again." 

現在你可以做的東西與my_time比較喜歡它:my_time == time.localtime()

,使運行,直到程序最簡單的方法「它的時間到了'如下:

import time 

while True: 
    if (my_time <= time.localtime()): 
     print "Running process" 
     process() 
     break 
    time.sleep(1) #Sleep for 1 second 

上面的例子絕不是最好的解決方案,但我ñ我認爲最容易實施。

另外我建議你使用http://docs.python.org/library/subprocess.html#subprocess.check_call來儘可能執行命令。

+0

當我嘗試你的代碼時出現這個錯誤:'AttributeError:'str'object has no attribute'strptime'' – Brian

+0

好的問題是我有一個變量叫做time ..我改變了這個。我更新了主要問題中的代碼。它的工作原理雖然不能正確輸出時間,並且過程不能運行。 – Brian

+0

問題在於:'if(selected_time == time):'你正在比較'selected_time'變量,它是一個'time.struct_time'對象和模塊'time'。相反,你應該做'if(selected_time == time.localtime())'我應該寫更好的例子。 –

相關問題