사용자가 지정한 특정 시간에 Python 스크립트 내에서 함수를 실행하려고합니다. 이렇게하려면 datetime 모듈을 사용하고 있습니다.시간 입력 및 사용자 입력과 비교
이 지금까지 코드의 일부입니다
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."
문제에서와 같이 (현재 시간을 현재 시간과 사용자 입력 시간을 비교하는 방법에 대한 방법을 찾기 위해 노력하고있다 필요하다 스크립트가 실행 중일 때). 또한, 어떻게 파이썬 스크립트를 실행하고 주어진 시간에 함수를 처리합니까?
코드를 업데이트하고 나에게 말한 것처럼 @alexandru Plugaru의 코드 제안을 사용했지만 올바르게 출력하지 못했습니다. process()가 실행되지 않습니다 – Brian
' time'과'selected_time'이 끝에 가깝습니다. 당신이해야 할 일은 델타와의 비교입니다 :'if selected_time> = current_time> = selected_time + delta' 그렇지 않으면 당신의 예정된 시간을 돌아 다닐 수 있습니다. 둘째, '시간'은 현재의 시간이 아닌 모듈이며, 비교가되지 않습니다. –
@Brian, Alexandru에게 질문에 대답 할 수 없어서 대답하겠습니다. 문제는 'selected_time'과 현재 시간이 동일 할 것으로 예상한다는 것입니다.귀하의 대기 시간이 5 초 단위로 이루어지기 때문에 이것은 발생하지 않을 것입니다. 비교에서'expected_time' 주위에 약간의 여백을 두어야합니다 (위의 내 코멘트 참조). –