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)))
python python-3.x subprocess temporary-files nano
add a comment |
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)))
python python-3.x subprocess temporary-files nano
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'snanothat creates your temp file again.
– DYZ
Nov 11 at 7:00
add a comment |
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)))
python python-3.x subprocess temporary-files nano
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
python python-3.x subprocess temporary-files nano
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'snanothat creates your temp file again.
– DYZ
Nov 11 at 7:00
add a comment |
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'snanothat 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
add a comment |
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
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 donedelete=Falsewere 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
|
show 3 more comments
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
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 donedelete=Falsewere 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
|
show 3 more comments
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
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 donedelete=Falsewere 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
|
show 3 more comments
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
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
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 donedelete=Falsewere 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
|
show 3 more comments
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 donedelete=Falsewere 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
|
show 3 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
nanothat creates your temp file again.– DYZ
Nov 11 at 7:00