Cannot find a file in my tempfile.TemporaryDirectory() for Python3

Viewed 2803

I'm having trouble working with Python3's tempfile library in general.

I need to write a file in a temporary directory, and make sure it's there. The third party software tool I use sometimes fails so I can't just open the file, I need to verify it's there first using a 'while loop' or other method before just opening it. So I need to search the tmp_dir (using os.listdir() or equivalent).

Specific help/solution and general help would be appreciated in comments.

Thank you.

Small sample code:

import os
import tempfile


with tempfile.TemporaryDirectory() as tmp_dir:

    print('tmp dir name', tmp_dir)

    # write file to tmp dir
    fout = open(tmp_dir + 'file.txt', 'w')
    fout.write('test write')
    fout.close()

    print('file.txt location', tmp_dir + 'lala.fasta')

    # working with the file is fine
    fin = open(tmp_dir + 'file.txt', 'U')
    for line in fin:
        print(line)

    # but I cannot find the file in the tmp dir like I normally use os.listdir()
    for file in os.listdir(tmp_dir):
        print('searching in directory')
        print(file)
1 Answers
Related