This is part of another script, too long for here, but isolating this part, my dilemma is that this works when manually invoked from IDLE, but not when run as a cron job.

from datetime import datetime
import os

# check to see if a log file exists, make one if it doesn't
if os.path.exists("C:\\Dir1\\Sub One\\Sub3\\Sub4\\Sub Five\\logfile.txt"):
        log = open('logfile.txt', 'r')
else:
    log = open('logfile.txt', 'w+')

The rest of the script runs as a cron and does its tasks, but this log never gets done, whether creating a new file because none exists or opening an existing logfile.

Any ideas why this isn't triggered when run as a cron?

Using IntellAdmin Windows Cron 2 Config on windows 7 machine.

"C:\Dir1\Sub One\Sub3\Sub4\Sub Five\logfile.txt"

Why are you using different names for logfile in the two different opens? Isn't it supposed to open file in same location?

I don't understand why you open the logfile in read mode in the first case. You can try the following, which should create an error file with a traceback if the logfile does not open

from datetime import datetime
import os
import traceback

logfilename = os.path.join('C:\\', 'Dir1', 'Sub One', 'Sub3', 'Sub4', 'Sub Five', 'logfile.txt')
mode = 'a' if os.path.exists(logfilename) else 'w+'
try:
    log = open(logfilename, mode)
except Exception:
    with open(os.path.join('C:\\', 'ERROR.txt'), 'w') as ofh:
        traceback.print_exc(file = ofh)

pyTony,

using different names for logfile in the two different opens

Not sure I understand here. Are you saying why the full path in the os.path.exists argument and only the file name in the opens? Because it looks like the same filename to me in the 2 opens. In each case "logfile.txt".

It does work manually as written, but then I'm already in that directory. Hmmm, so if I put full path in the opens you think it might work?? I'll try that.

Gribouillis,

I haven't any experience with exceptions yet, but will also give your example a try. I like that os.path.join function. Much easier to read and understand.

Thanks

Got it to work using the os methods. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.