[Python] Inputting the time and comparing it with user input

Discussão em 'Python' iniciado por Stack, Maio 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to make a function run within a Python script at a certain time given by the user. To do this I'm using the datetime module.

    This is part of the code so far:

    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."


    The problem is having is trying to find a way on how to compare the user inputted time with the current time (as in, the current time for when the script is running). Also, how do I keep a python script running and process a function at the given time?

    Continue reading...

Compartilhe esta Página