Python Forum
Need help to prove close File
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help to prove close File
#1
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
Reply
#2
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?
Reply
#3
(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).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
(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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
OK
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Save and Close Excel File avd88 0 2,996 Feb-20-2023, 07:19 PM
Last Post: avd88
  pyPDF2 PDFMerger close pensding file japo85 2 2,402 Jul-28-2022, 09:49 AM
Last Post: japo85
  How to close file handler or log file on each iteration Mekala 3 5,084 Aug-16-2020, 03:15 PM
Last Post: snippsat
  How To Find an Opening and Closing String, Copying Open/Close/Contents to New File davidshq 1 2,021 Mar-03-2020, 04:47 AM
Last Post: davidshq
  How can I Open and close .py file from python scripts SayHiii 9 5,730 Dec-17-2019, 06:10 AM
Last Post: Malt
  Does python guarantees that file.write() works without file.close() ? kryptomatrix 7 3,909 Oct-21-2019, 11:28 AM
Last Post: kryptomatrix
  should I close csv file? go127a 8 16,394 May-07-2019, 01:16 PM
Last Post: go127a

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020