external file closed

Discussion in 'Python' started by kilnhead, Oct 17, 2006.

  1. kilnhead

    kilnhead Guest

    I am opening a file using os.start('myfile.pdf') from python. How can I
    know when the user has closed the file so I can delete it? Thanks.
    kilnhead, Oct 17, 2006
    #1
    1. Advertising

  2. kilnhead

    Jerry Guest

    On Oct 17, 12:43 pm, "kilnhead" <> wrote:
    > I am opening a file using os.start('myfile.pdf') from python. How can I
    > know when the user has closed the file so I can delete it? Thanks.


    I assume you mean os.startfile. There is no way to do this directly.
    os.startfile simply hands off the call to the OS and doesn't provide
    anything to track anything after that. Since you won't know what
    program handled the file association, you couldn't watch for an
    instance of that to start up and detect when it exits. Even if you
    could, it wouldn't be reliable as in the case of PDF's and Adobe
    Acrobat Reader, the user could close the document, but not the
    application, so your script would never delete the file in question.

    If anyone can think of a way to do this, it would be interesting to see
    how it's done.

    --
    Jerry
    Jerry, Oct 17, 2006
    #2
    1. Advertising

  3. kilnhead

    utabintarbo Guest

    Jerry wrote:
    > On Oct 17, 12:43 pm, "kilnhead" <> wrote:
    > > I am opening a file using os.start('myfile.pdf') from python. How can I
    > > know when the user has closed the file so I can delete it? Thanks.

    >
    > I assume you mean os.startfile. There is no way to do this directly.
    > os.startfile simply hands off the call to the OS and doesn't provide
    > anything to track anything after that. Since you won't know what
    > program handled the file association, you couldn't watch for an
    > instance of that to start up and detect when it exits. Even if you
    > could, it wouldn't be reliable as in the case of PDF's and Adobe
    > Acrobat Reader, the user could close the document, but not the
    > application, so your script would never delete the file in question.
    >
    > If anyone can think of a way to do this, it would be interesting to see
    > how it's done.
    >
    > --
    > Jerry


    os.system('myfile.pdf') will give return code upon closing. This can
    also be done using the subprocess module with poll().
    utabintarbo, Oct 17, 2006
    #3
  4. kilnhead

    Guest

    You may be able to use os.popen()
    kilnhead wrote:
    > I am opening a file using os.start('myfile.pdf') from python. How can I
    > know when the user has closed the file so I can delete it? Thanks.
    , Oct 17, 2006
    #4
  5. kilnhead

    Jordan Guest

    I think the win32all extension includes the findwindow() fuction, so
    you could make a loop that looks for the window name (or class if it
    takes that) of the pdf. You can also loop through a list of running
    processes looking for whatever the process name is. Note that both of
    these have serious loopholes, such as if there is more than one pdf
    open.

    Cheers,
    Jordan

    utabintarbo wrote:
    > Jerry wrote:
    > > On Oct 17, 12:43 pm, "kilnhead" <> wrote:
    > > > I am opening a file using os.start('myfile.pdf') from python. How can I
    > > > know when the user has closed the file so I can delete it? Thanks.

    > >
    > > I assume you mean os.startfile. There is no way to do this directly.
    > > os.startfile simply hands off the call to the OS and doesn't provide
    > > anything to track anything after that. Since you won't know what
    > > program handled the file association, you couldn't watch for an
    > > instance of that to start up and detect when it exits. Even if you
    > > could, it wouldn't be reliable as in the case of PDF's and Adobe
    > > Acrobat Reader, the user could close the document, but not the
    > > application, so your script would never delete the file in question.
    > >
    > > If anyone can think of a way to do this, it would be interesting to see
    > > how it's done.
    > >
    > > --
    > > Jerry

    >
    > os.system('myfile.pdf') will give return code upon closing. This can
    > also be done using the subprocess module with poll().
    Jordan, Oct 17, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. damezumari
    Replies:
    2
    Views:
    498
    damezumari
    Apr 27, 2006
  2. Scott Allen
    Replies:
    8
    Views:
    10,929
    Scott Allen
    May 2, 2004
  3. Matt Kruse
    Replies:
    5
    Views:
    307
    Richard Cornford
    Sep 9, 2003
  4. Brad Baxter

    do 'file' -- when is file closed?

    Brad Baxter, Apr 5, 2006, in forum: Perl Misc
    Replies:
    7
    Views:
    100
    A. Sinan Unur
    Apr 5, 2006
  5. Mellow Crow
    Replies:
    6
    Views:
    416
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page