Python Forum
Need help to prove close File - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Need help to prove close File (/thread-3362.html)



Need help to prove close File - slice - May-17-2017

There is a task to open the file (not in the python), let it be txt or word or whatever, for this we use


import os

os.startfile ('NEWS.txt')
Ok, we opened it
How then, after closing, to prove that it is closed?

I tried to use
import psutil
And watch the processes, but if another file is opened and the previous one is closed, and the process is one. help me please


RE: Need help to prove close File - Larz60+ - May-17-2017

From the docs:
Quote:startfile returns as soon as the associated application is launched.
There is no option to wait for the application to close, and no way
to retrieve the application's exit status.

Why are you using os.startfile? Is it not possible to use standard file open?


RE: Need help to prove close File - Ofnuts - May-18-2017

(May-17-2017, 05:38 PM)slice Wrote: There is a task to open the file (not in the python), let it be txt or word or whatever, for this we use


import os

os.startfile ('NEWS.txt')
Ok, we opened it
How then, after closing, to prove that it is closed?

I tried to use
import psutil
And watch the processes, but if another file is opened and the previous one is closed, and the process is one. help me please

On Windows, you shouldn't be able to rename it if it is open (but you can on Linux). On all platforms, in most cases you won't be able to open it for writing (in binary and append, more of course) if it's use by some other process (unless said process explicitly allows it).

However, there may  be applications that open the file, read it, and close it immediately before working on the contents, and that reopen the file to write modified contents when finished. In such case you'll be able to open the file even though the other process isn't really done with it.
You may want to mix this with a check for the file modification timestamp (at least if you expect an update).


RE: Need help to prove close File - Ofnuts - May-18-2017

(May-17-2017, 07:17 PM)Larz60+ Wrote: From the docs:
Quote:startfile returns as soon as the associated application is launched.
   There is no option to wait for the application to close, and no way
   to retrieve the application's exit status.

Why are you using os.startfile? Is it not possible to use standard file open?

startfile() opens the file with the default application, it is not meant to read the file contents from Python.


RE: Need help to prove close File - Larz60+ - May-18-2017

OK