Why am I able to write to and read a tempfile even after closing it?











up vote
1
down vote

favorite












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)))









share|improve this question
























  • From the docs "If delete is true (the default), the file is deleted as soon as it is closed."
    – raj
    Nov 11 at 6:38








  • 1




    I cannot reproduce your error. The file is deleted as soon as it is closed.
    – DYZ
    Nov 11 at 6: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?
    – raj
    Nov 11 at 6:42










  • Ok. It's nano that creates your temp file again.
    – DYZ
    Nov 11 at 7:00















up vote
1
down vote

favorite












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)))









share|improve this question
























  • From the docs "If delete is true (the default), the file is deleted as soon as it is closed."
    – raj
    Nov 11 at 6:38








  • 1




    I cannot reproduce your error. The file is deleted as soon as it is closed.
    – DYZ
    Nov 11 at 6: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?
    – raj
    Nov 11 at 6:42










  • Ok. It's nano that creates your temp file again.
    – DYZ
    Nov 11 at 7:00













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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)))









share|improve this question















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)))






python python-3.x subprocess temporary-files nano






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 6:55

























asked Nov 11 at 6:34









raj

592418




592418












  • From the docs "If delete is true (the default), the file is deleted as soon as it is closed."
    – raj
    Nov 11 at 6:38








  • 1




    I cannot reproduce your error. The file is deleted as soon as it is closed.
    – DYZ
    Nov 11 at 6: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?
    – raj
    Nov 11 at 6:42










  • Ok. It's nano that creates your temp file again.
    – DYZ
    Nov 11 at 7:00


















  • From the docs "If delete is true (the default), the file is deleted as soon as it is closed."
    – raj
    Nov 11 at 6:38








  • 1




    I cannot reproduce your error. The file is deleted as soon as it is closed.
    – DYZ
    Nov 11 at 6: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?
    – raj
    Nov 11 at 6:42










  • Ok. It's nano that creates your temp file again.
    – DYZ
    Nov 11 at 7:00
















From the docs "If delete is true (the default), the file is deleted as soon as it is closed."
– raj
Nov 11 at 6:38






From the docs "If delete is true (the default), the file is deleted as soon as it is closed."
– raj
Nov 11 at 6:38






1




1




I cannot reproduce your error. The file is deleted as soon as it is closed.
– DYZ
Nov 11 at 6:40




I cannot reproduce your error. The file is deleted as soon as it is closed.
– DYZ
Nov 11 at 6: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?
– raj
Nov 11 at 6:42




@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?
– raj
Nov 11 at 6:42












Ok. It's nano that creates your temp file again.
– DYZ
Nov 11 at 7:00




Ok. It's nano that creates your temp file again.
– DYZ
Nov 11 at 7:00












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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





share|improve this answer





















  • 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?
    – raj
    Nov 11 at 6: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 at 6:59






  • 1




    @raj A temp file is opened in /tmp.
    – DYZ
    Nov 11 at 6:59










  • @DYZ Not necessarily, for example you can also change the path to the workingdirectory (linux: stackoverflow.com/questions/31068/… windows: answers.microsoft.com/en-us/windows/forum/windows_7-files/…) - but in most cases you are right
    – quant
    Nov 11 at 7: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?
    – raj
    Nov 11 at 7:04











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246421%2fwhy-am-i-able-to-write-to-and-read-a-tempfile-even-after-closing-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










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





share|improve this answer





















  • 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?
    – raj
    Nov 11 at 6: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 at 6:59






  • 1




    @raj A temp file is opened in /tmp.
    – DYZ
    Nov 11 at 6:59










  • @DYZ Not necessarily, for example you can also change the path to the workingdirectory (linux: stackoverflow.com/questions/31068/… windows: answers.microsoft.com/en-us/windows/forum/windows_7-files/…) - but in most cases you are right
    – quant
    Nov 11 at 7: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?
    – raj
    Nov 11 at 7:04















up vote
2
down vote



accepted










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





share|improve this answer





















  • 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?
    – raj
    Nov 11 at 6: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 at 6:59






  • 1




    @raj A temp file is opened in /tmp.
    – DYZ
    Nov 11 at 6:59










  • @DYZ Not necessarily, for example you can also change the path to the workingdirectory (linux: stackoverflow.com/questions/31068/… windows: answers.microsoft.com/en-us/windows/forum/windows_7-files/…) - but in most cases you are right
    – quant
    Nov 11 at 7: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?
    – raj
    Nov 11 at 7:04













up vote
2
down vote



accepted







up vote
2
down vote



accepted






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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 6:53









quant

1,53711526




1,53711526












  • 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?
    – raj
    Nov 11 at 6: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 at 6:59






  • 1




    @raj A temp file is opened in /tmp.
    – DYZ
    Nov 11 at 6:59










  • @DYZ Not necessarily, for example you can also change the path to the workingdirectory (linux: stackoverflow.com/questions/31068/… windows: answers.microsoft.com/en-us/windows/forum/windows_7-files/…) - but in most cases you are right
    – quant
    Nov 11 at 7: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?
    – raj
    Nov 11 at 7:04


















  • 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?
    – raj
    Nov 11 at 6: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 at 6:59






  • 1




    @raj A temp file is opened in /tmp.
    – DYZ
    Nov 11 at 6:59










  • @DYZ Not necessarily, for example you can also change the path to the workingdirectory (linux: stackoverflow.com/questions/31068/… windows: answers.microsoft.com/en-us/windows/forum/windows_7-files/…) - but in most cases you are right
    – quant
    Nov 11 at 7: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?
    – raj
    Nov 11 at 7:04
















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?
– raj
Nov 11 at 6:57




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?
– raj
Nov 11 at 6: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 at 6:59




@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 at 6:59




1




1




@raj A temp file is opened in /tmp.
– DYZ
Nov 11 at 6:59




@raj A temp file is opened in /tmp.
– DYZ
Nov 11 at 6:59












@DYZ Not necessarily, for example you can also change the path to the workingdirectory (linux: stackoverflow.com/questions/31068/… windows: answers.microsoft.com/en-us/windows/forum/windows_7-files/…) - but in most cases you are right
– quant
Nov 11 at 7:01




@DYZ Not necessarily, for example you can also change the path to the workingdirectory (linux: stackoverflow.com/questions/31068/… windows: answers.microsoft.com/en-us/windows/forum/windows_7-files/…) - but in most cases you are right
– quant
Nov 11 at 7: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?
– raj
Nov 11 at 7:04




@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?
– raj
Nov 11 at 7:04


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246421%2fwhy-am-i-able-to-write-to-and-read-a-tempfile-even-after-closing-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly