简体   繁体   中英

os.makedirs works when run in IDLE, fails in scheduled task

I have an ArcPy script that works when run in IDLE, but fails when run as a scheduled task. This is the problem section:

outPath = os.path.abspath(r"X:\GroupDirs\0731\GIS_CORE\PUPS")
log.write("Output path: {}\n".format(outPath))
try:
    outDir = os.path.join(outPath, "utiliquest{}".format(date))
    if not os.path.exists(outDir):
        os.makedirs(outDir)
    log.write("Created scratch and output")
except:
    fail_log(log)

When I run this in IDLE, it outputs correctly, makes the directory, and continues to execute.

Output path: X:\GroupDirs\0731\GIS_CORE\PUPS
Created scratch and output

When this runs as a scheduled task, the logfile has the correct directory, but the os.makedirs(outDir) command fails to execute:

Output path: X:\GroupDirs\0731\GIS_CORE\PUPS

     Failed at 09/24/14 14:41:45
PYTHON ERRORS:
Traceback info:
  File "C:\Batch Processes\IRTHnet\Batch_CopyData.py", line 64, in <module>
    os.makedirs(outDir)

Error Info:
[Error 3] The system cannot find the path specified: 'X:\\'

This is being run on the same machine each time, so the X drive is pointing to the same location.

Why is this causing a failure, and what I can do to fix/circumvent?

This is being run on the same machine each time, so the X drive is pointing to the same location.

That doesn't follow. Drive mappings are per-session. If you've set up Window to restore your drive mappings at logon (which is what most people do, since at least in XP through 7 there's a checkbox for "Reconnect at logon" in the "Map Drive" dialog in Explorer), then the X drive won't be pointing to the same location unless you've logged on as the same user.

You can set up system-wide drive mappings that will be reconnected at startup instead of logon, but this isn't trivial (and may be different on different Windows versions), and it also leads to a mess of permissions issues that you really don't want to deal with.

See Services and Redirected Drives at MSDN, which isn't directly what you're trying to do here, but does explain the issues pretty nicely and link to a lot of other useful documentation.

An alternative solution is to just use a UNC path, like r'\\\\Server\\share\\GroupDirs\\0731\\GIS_CORE\\PUPS' . This isn't always appropriate (eg, you might want to mount different shares to X: on different machines, and have your script always use whichever one is mounted to X: ), but when it is, it's usually simplest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM