2

I was experimenting with opening text editors from my python script and I noticed something that apparently contradicts my understanding of the documentation of tempfile.

My experiment started out with Alex Martelli's answer.
My code -

import os
import tempfile
import subprocess

f = tempfile.NamedTemporaryFile(mode='w+t', delete=True)
n = f.name
print('Does exist? : {0}'.format(os.path.exists(n)))
f.close()
print('Does exist? : {0}'.format(os.path.exists(n)))

subprocess.run(['nano', n])
with open(n) as f:
    print (f.read())

print('Does exist? : {0}'.format(os.path.exists(n)))

OUTPUT:

Does exist? : True
Does exist? : False
Hello from temp file.

Does exist? : True

In the code, I explicitly call close on the file object declared with delete=True, however even then I am able to write and read contents to it. I don't understand why this is happening. According to the docs-

If delete is true (the default), the file is deleted as soon as it is closed.

If calling close deletes the file then I SHOULD NOT be able to write and then read it. But it displays the correct contents of the file that you enter when nano executes. And like a tempfile, the file is not visible in the directory where I opened the terminal and ran the script. What is even more strange is that os.path.exists works correctly for the first two times and possibly incorrectly for the third time.
Am I missing something here?

Additional Experiment:
If I run the following code then I can clearly see the file created. But that doesn't happen in the original code.

n = '.temp'
subprocess.run(['nano', n])
with open(n) as f:
    print (f.read())

print('Does exist? : {0}'.format(os.path.exists(n)))
jar
  • 2,646
  • 1
  • 22
  • 47
  • From the docs "If delete is true (the default), the file is deleted as soon as it is closed." – jar Nov 11 '18 at 06:38
  • 1
    I cannot reproduce your error. The file is deleted as soon as it is closed. – DYZ Nov 11 '18 at 06:40
  • @DYZ i am sorry, i forgot to add output. Edited now. There is NO error. The issue is that It SHOULD NOT work. Why is it working? – jar Nov 11 '18 at 06:42
  • Ok. It's `nano` that creates your temp file again. – DYZ Nov 11 '18 at 07:00

1 Answers1

3

Lets have a deeper look at your code.

First you create your temp file

f = tempfile.NamedTemporaryFile(mode='w+t', delete=True)
n = f.name
print('Does exist? : {0}'.format(os.path.exists(n)))

and this output

Does exist? : True

so there is nothing to worry about. Then in the next statements

f.close()
print('Does exist? : {0}'.format(os.path.exists(n)))

you are closing the file and actually the file gets deleted, because you are getting the following output:

Does exist? : False

Afterwards however you are recreating your file via

subprocess.run(['nano', n])
with open(n) as f:
    print (f.read())

so this is why afterwards the command

print('Does exist? : {0}'.format(os.path.exists(n)))

returns

Does exist? : True
quant
  • 2,184
  • 2
  • 19
  • 29
  • Yes, exactly. But then why can't I actually see the file in the working directory? I have edited the question to add this information. I write only the last part of the code and there I can see that it creates the temp file. But why is it not visible in the original one? – jar Nov 11 '18 at 06:57
  • @raj tempfile isn't creating the file in the working directory but in the temp file directory. So you will never see the file in the working directory. – quant Nov 11 '18 at 06:59
  • 1
    @raj A temp file is opened in `/tmp`. – DYZ Nov 11 '18 at 06:59
  • @DYZ Not necessarily, for example you can also change the path to the workingdirectory (linux: https://stackoverflow.com/questions/31068/how-do-i-find-the-temp-directory-in-linux windows: https://answers.microsoft.com/en-us/windows/forum/windows_7-files/change-location-of-temp-files-folder-to-another/19f13330-dde1-404c-aa27-a76c0b450818) - but in most cases you are right – quant Nov 11 '18 at 07:01
  • @DYZ yes, I just checked my /tmp folder and the files for which I had done `delete=False` were there. I just wanted to ask, if these files will be there forever? Also, since this answers my question and my question kind of looks silly now, should i leave it or delete it or accept @quant 's answer? – jar Nov 11 '18 at 07:04
  • @raj You may leave the question here. Then, whenever someone has a similar question, I can simply mark his question as duplicated. – quant Nov 11 '18 at 07:07
  • @quant okay. I will however accept your answer and maybe add one of my own making a summary of our comments if thats okay? – jar Nov 11 '18 at 07:09
  • @ray For your point "and my question kind of looks silly now" ... You did not know that temp folders are created in the temp. directory - so I don't consider your question as silly or stupid. – quant Nov 11 '18 at 07:09