简体   繁体   中英

tempfile.NamedTemporaryFile crashing in Task Scheduler

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)

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

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