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

问题

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值