python script won't finish in task scheduler

Deadly 提交于 2020-01-15 04:54:12

问题


I have the following python script below that will execute if I run the module manually, but when I try to schedule it in Windows Task Scheduler, it doesn't finish. I have a log file being exported as part of the script and it prints down to 'Start Time' but doesn't make it to 'End Time' which seems to suggest it's getting stuck on the copy function.

Task scheduler settings:

1) Run whether user is logged on or not

2) Run with highest privileges

import os
import sys
import shutil
import glob
import datetime
import time
from time import localtime, strftime

# Directories
dir_src = "W:\\Engineer\\DO NOT USE - Entrance Information"
dir_dst = "C:\\data\\engineering\\EntranceInformation"

# Export print messages to text file
time_now = strftime("%Y-%m-%d_%H-%M-%S", localtime())
logfile = 'C:\\logs\\Engineer Entrances\\'+time_now+'.txt'
f = open(logfile, "w")

# Name of script
script = "CopyPasteEngineerEntrancePDF.py"
print >>f, "Script: ", script

# Get start time
start_time = datetime.datetime.now()
start_time_readable = strftime("%Y-%m-%d %H.%M.%S", localtime())
print >>f, "Start Time: ", start_time_readable

# Copy files
for file in os.listdir(dir_src):
    print >>f, file
    if file.endswith(".pdf"):
            print >>f, file
            src_file = os.path.join(dir_src, file)
            dst_file = os.path.join(dir_dst, file)
            shutil.copy(src_file, dst_file)
            print >>f, "Source File: ", src_file
            print >>f, "Destination File: ", dst_file

# Get end time
end_time = datetime.datetime.now()
end_time_readable = strftime("%Y-%m-%d %H:%M:%S", localtime())
print >>f, "End Time: ", end_time_readable

# Get elapsed time
elapsed_time = datetime.datetime.now() - start_time
print >>f, "Elapsed Time: ", elapsed_time

# Get count of files copied
file_count = len(glob.glob1(dir_dst,"*.pdf"))
print >>f, "Files Copied: ", file_count

# Close text file
f.close()

来源:https://stackoverflow.com/questions/20636844/python-script-wont-finish-in-task-scheduler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!