0

I have a Python script that downloads a txt file from an SFTP site, changes the headers, writes a new text file then converts that to CSV. This script runs perfectly in IDLE. I'm trying to set this up as a daily task in Windows Task Scheduler and it's failing with this error: IOError: [Errno 17] No usable temporary file name found.

Here is the relevant section of the code. headerChangeDict is defined above this section. The error comes from the first line here:

with tempfile.NamedTemporaryFile(dir='.', delete=False) as tmp,\
    open(spaces_txt_local_filepath, 'rb') as f:
    r = csv.reader(f, delimiter = '\t')
    w = csv.writer(tmp, delimiter = '\t', quoting=csv.QUOTE_NONNUMERIC)
    header = next(r)
    for h in header:
        newHeader = re.sub("\s+", "_", h.strip())

        for headerChangeStr in headerChangeDict.keys():
            if newHeader == headerChangeStr:
                newHeader = headerChangeStr.replace(headerChangeStr,headerChangeDict[headerChangeStr])

        newHeaderList.append(newHeader)

    w.writerow(newHeaderList)

    for row in r:
        w.writerow(row)

os.rename(tmp.name, new_text_filepath)
martineau
  • 119,623
  • 25
  • 170
  • 301
Bill Hambone
  • 157
  • 2
  • 14
  • It could have something to do the credentials being used to run the scheduled task. See this [answer](https://stackoverflow.com/a/4209102/355230) to a related qustion I wrote long ago on the subject. – martineau Dec 03 '18 at 22:09
  • OK. Python is not installed at the system level, only on my user account. – Bill Hambone Dec 03 '18 at 22:16
  • Are you setting environment variables TMP or TEMP. I think `tempfile` uses them. They must be writable by the effective user who runs your scheduled task – phil Dec 03 '18 at 22:29
  • Yeah, they're both under user\AppData\local\Temp – Bill Hambone Dec 03 '18 at 22:36
  • I tried making sure the Temp directory wasn't read-only and that my user profile has full permissions and it didn't help. I've had this kind of trouble with task scheduler before. Is there a good free alternative? – Bill Hambone Dec 03 '18 at 22:46

1 Answers1

1

Ah, I got it. I needed to change dir='.' to the specified temp path where this script is writing the text and csv files.

Bill Hambone
  • 157
  • 2
  • 14
  • Make sure it's on the same volume as `new_text_filepath`. We can't do a cross-volume `rename`. – Eryk Sun Dec 04 '18 at 00:19
  • It is the same volume. Now i need to figure out how to get task scheduler to work when I check "Run whether user is logged on or not" – Bill Hambone Dec 04 '18 at 00:22
  • Running without a pre-existing interactive logon uses a service-for-user (S4U) batch logon in the services session, so the task won't have access to cached network logon credentials and mapped drives, and it won't be able to interact with the user or display an interface. – Eryk Sun Dec 04 '18 at 05:34
  • How do I set this up? – Bill Hambone Dec 04 '18 at 17:25
  • I've tried running Task Scheduler as an administrator and recreating the task to no avail. I've tried changing user to SYSTEM, and that didn't work. – Bill Hambone Dec 04 '18 at 17:36
  • Ah, I think the issue is that I've installed Python modules under my user profile and the system can't access them. – Bill Hambone Dec 04 '18 at 17:40
  • You can create a task to run non-interactively as any user that has the batch logon right. Administrators have this right. Standard users do not have it, unless it's explicitly granted to them. – Eryk Sun Dec 04 '18 at 18:00
  • OK. How would I go about setting this up? Thanks! – Bill Hambone Dec 04 '18 at 18:04
  • The only way that's on topic here is via the scheduler's COM API, which is possible in Python with the PyWin32 extensions. I suppose using schtasks.exe is somewhat on topic for scripting. But questions about using the scheduler's graphical interface should be asked on superuser.com or serverfault.com. – Eryk Sun Dec 04 '18 at 18:09