0

I used this python function to open a picture using a python script:

import os
class ReadPicture:
    def readPicture(self):
        os.startfile('picture.jpg')
if __name__=='__main__':
    RP.ReadPicture()
    RP.readPicture()

The image is opened with its default application on Windows. Can you tell me how to close the opened picture now ? Thanks for any suggestions.

Why do not I use PIL ? Because it does not do what I want:

When operation is not specified or 'open', this acts like double-clicking the file in Windows Explorer

Yes, I want to open the picture as in a double-click behavior.

2 Answers2

1

I think that's not possible. os.startfile() doesn't return anything, so you can't find out which process was started (and sometimes, Windows might reuse an existing process, so killing it would lose work).

If you want to be able to close the window with the image, you need to do is to load the image using a framework like PIL and then write your own UI to display using Tkinter or PyQt

Another alternative is to look at pywin32 which gives you access to the Windows API. With that, you can reimplement os.startfile() in Python. That way, you might be able to control the child process better.

Related:

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 1
    But my aim is to reproduce the double-click behavior as os.startfile() does. So my question: does PIL, Tkinter or PyQT have this behavior ? –  Jul 24 '14 at 08:19
  • 1
    And I do not want a GUI for my program because it needs to run *silently* (invisible) –  Jul 24 '14 at 08:35
  • @emptyheaded: Then you'll have to use the `win32` module and write your own version of `os.startfile()`. See my edits. – Aaron Digulla Jul 24 '14 at 08:51
0
#008_py_process_kill_proc_cmdline_dispaly
import os
pidl=[pid for pid in os.listdir('/proc') if pid.isdigit()]
print(pidl[-10:]) # pid_list recent 10 process from/proc directory
cnt=0
for pid in pidl:
    path_and_file_=os.path.join('/proc/', pid, 'cmdline')
    print(path_and_file)
    cmdf=open(path_and_file)
    cmdl=cmdf.readline() #read first one line in cmdline-file
    print('pid=',pid, 'cmdline=', cmdl)
    if(len(cmdl)>=6):
        if(cmdl[0]=='d' and cmdl[1]=='i'): # 'disply' image show() name
            os.system('kill -9 '+ pid)
            cnt +=1
print('killed total= %d'%cnt)