2

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()
boyle.matt
  • 191
  • 1
  • 3
  • 10
  • Put in some more `print` statements throughout the `# Copy files` section to see exactly where it gets to. Also print the values of `src_file` and `dst_file`. – dg99 Dec 17 '13 at 16:45
  • added print statements for src_file and dst_file. log file looks correct, showing the correct source file and correct destination file. works when running manually. – boyle.matt Dec 17 '13 at 17:09
  • tried task scheduler...log file didn't show source file or destination file. – boyle.matt Dec 17 '13 at 17:15
  • Keep adding debugging statements. Print the name of every file in the `os.listdir` result (i.e. before the `if file.endswith` part), and then again inside the PDF section. See which files make it inside that `if` statement. – dg99 Dec 17 '13 at 19:16
  • i'm using python 2.7...is the version an issue with any of the modules? – boyle.matt Dec 17 '13 at 22:05
  • Shouldn't be. Did you try adding more debugging statements? – dg99 Dec 17 '13 at 22:31
  • Edited the question to show the added print statements. I'm pretty new to python so I'm not sure what exactly to add to the print statements...i.e. is there better code that could be used? – boyle.matt Dec 17 '13 at 22:49

0 Answers0