Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

temp

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)
like image 664
JTFouquier Avatar asked Jul 07 '17 15:07

JTFouquier


Video Answer


1 Answers

That's expected because the temporary directory name doesn't end with path separator (os.sep, slash or backslash on many systems). So the file is created at the wrong level.

tmp_dir = D:\Users\foo\AppData\Local\Temp\tmpm_x5z4tx
tmp_dir + "file.txt"
=> D:\Users\foo\AppData\Local\Temp\tmpm_x5z4txfile.txt

Instead, join both paths to get a file inside your temporary dir:

fout = open(os.path.join(tmp_dir,'file.txt'), 'w')

note that fin = open(tmp_dir + 'file.txt', 'U') finds the file, that's expected, but it finds it in the same directory where tmp_dir was created.

like image 197
Jean-François Fabre Avatar answered Oct 24 '22 08:10

Jean-François Fabre