How do I close a file opened using os.startfile(), Python 3.6

核能气质少年 提交于 2020-06-23 05:09:29

问题


I want to close some files like .txt, .csv, .xlsx that I have opened using os.startfile().

I know this question asked earlier but I did not find any useful script for this.

I use windows 10 Environment


回答1:


I believe the question wording is a bit misleading - in reality you want to close the app you opend with the os.startfile(file_name)

Unfortunately, os.startfile does not give you any handle to the returned process. help(os.startfile)

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.

Luckily, you have an alternative way of opening a file via a shell:

shell_process = subprocess.Popen([file_name],shell=True) 
print(shell_process.pid)

Returned pid is the pid of the parent shell, not of your process itself. Killing it won't be sufficient - it will only kill a shell, not the child process. We need to get to the child:

parent = psutil.Process(shell_process.pid)
children = parent.children(recursive=True)
print(children)
child_pid = children[0].pid
print(child_pid)

This is the pid you want to close. Now we can terminate the process:

os.kill(child_pid, signal.SIGTERM)
# or
subprocess.check_output("Taskkill /PID %d /F" % child_pid)

Note that this is a bit more convoluted on windows - there is no os.killpg More info on that: How to terminate a python subprocess launched with shell=True

Also, I received PermissionError: [WinError 5] Access is denied when trying to kill the shell process itself with os.kill

os.kill(shell_process.pid, signal.SIGTERM)

subprocess.check_output("Taskkill /PID %d /F" % child_pid) worked for any process for me without permision error See WindowsError: [Error 5] Access is denied




回答2:


Based on this SO post, there's no way to close the file being opened with os.startfile(). Similar things are discussed in this Quora post.

However, as is suggested in the Quora post, using a different tool to open your file, such as subprocess or open(), would grant you greater control to handle your file.

I assume you're trying to read in data, so in regards to your comment about not wanting to close the file manually, you could always use a with statement, e.g.

with open('foo') as f:
    foo = f.read()

Slightly cumbersome, as you would have to also do a read(), but it may suit your needs better.




回答3:


In order to properly get the pid of children,you may add a while cycle


import subprocess
import psutil
import os
import time
import signal
shell_process = subprocess.Popen([r'C:\Pt_Python\data\1.mp4'],shell=True)
parent = psutil.Process(shell_process.pid)
while(parent.children() == []):
    continue
children = parent.children()
print(children)


来源:https://stackoverflow.com/questions/57909525/how-do-i-close-a-file-opened-using-os-startfile-python-3-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!