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

解决方案

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

python关闭文件os_如何关闭使用os.startfile(),Python 3.6打开的文件相关推荐

  1. 如何复制权限受限PDF文件的内容(亲测有效,Microsoft Edge打开pdf文件)

    如何复制权限受限PDF文件的内容(亲测有效,Microsoft Edge打开pdf文件) 当我们阅读大型pdf文档资料时,会有做笔记的习惯,刚开始打字做笔记还好,但后面发现有用的内容好像有点多,于是选 ...

  2. word文档中找不到服务器,【在WORD文件中插入excel表格,编辑保存后重新打开word文件并双击excel时提示“找不到服务器应用程序、源文件】excel整个表格不见了...

    为什么PPT插入EXCEL表格时显示无法找到服务器应用程序 这是由于excel或者ppt只能兼容.低版本的ppt和excel是无法打版本excel插入的表格的. 解决如下: 1.新建一个文本,将后缀名 ...

  3. 启动python项目找不到路径_Python os.startfile找不到FI

    我对Python编程有问题.在我的程序中,我使用os.startfile()来启动程序,但它不允许我这样做. 当我的程序执行启动文件的命令时,我在Windows脚本主机中收到以下错误:Script: ...

  4. 搜索计算机文件夹的记录怎么删除,怎样删除电脑最近打开的文件或文件夹?

    当我们每次打开电脑,单击"开始"后用鼠标指向"文档"时,都会显示出一大串我们以前打开过的文档记录,其中包含Word.图片.压缩包.多媒体文件等等.这些打开的文件 ...

  5. 文件所在的卷已被外部更改,因此打开的文件不再有效

    改了一下,问题解决了,但是到现在都不知道原因是什么,先贴上来看看,希望以后能明白 原代码: #include<stdio.h> int main(){int max(int,int);in ...

  6. Python(10)文件操作(IO 技术)、OS模块、递归

    文章目录 文件操作(IO 技术) 文本文件和二进制文件 文件操作相关模块概述 创建文件对象 open() 基本的文件写入操作 常用编码介绍 ASCII ISO8859-1 GB2312 GBK GB1 ...

  7. Python文件操作-文本文件、二进制文件、csv文件的读取写入、OS、shutil、CSV模块、常用字符编码

    Python文件操作 文本文件和二进制文件 文件操作相关模块 open()创建文件对象 文件对象的常用属性和方法 pickle 序列化 文本文件读取和写入 文本文件写入步骤 write()/write ...

  8. python的read函数调用报错_从零开始学Python(七):文件存储I/O流和异常捕捉

    这两天在搞小程序开发,耽搁了一下更新的进度,今天补上.一个完整的前端程序肯定离不开数据存储和网络两个模块,今天我们就先来讲讲python中的一种常见数据存储:文件存储!~! 文件存储(I/O操作) 何 ...

  9. 不是python文件操作的相关函数_从零开始学Python(七):文件存储I/O流和异常捕捉

    这两天在搞小程序开发,耽搁了一下更新的进度,今天补上.一个完整的前端程序肯定离不开数据存储和网络两个模块,今天我们就先来讲讲python中的一种常见数据存储:文件存储!~! 文件存储(I/O操作) 何 ...

最新文章

  1. Intellij IDEA 快捷键整理
  2. 鸟哥的Linux私房菜(基础篇)-第二章、 Linux 如何学习(二.5. 重点回顾)
  3. 我是如何在阿里巴巴面试中壮烈牺牲的?(内含面试题)
  4. 武大计算机学院推免清北,南大2020年推免生来自哪儿?山大73,武大41,清北5人...
  5. Locality Sensitive Hashing
  6. log4j2 pattern 行号_Springboot整合log4j2日志全解总结
  7. 第4章 Python 数字图像处理(DIP) - 频率域滤波7 - 二维DFT和IDFT的一些性质 - 傅里叶频谱和相角
  8. MQTT客户端库-Paho GO
  9. ios nslog 例子_iOS 典型内存泄露案例 - zhenshan2013的个人空间 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
  10. 惠普z6计算机进不去桌面,HP Z6 桌面工作站 | HP® HK 惠普香港
  11. 基于JAVA+SpringMVC+Mybatis+MYSQL的个人相册管理系统
  12. ffmpeg源码分析_ffmpeg音视频同步的几种策略
  13. 系统优化的方法有哪些
  14. 什么是延展性(Malleability,可鍛性)
  15. 工程师们,不要想一辈子靠技术混饭吃
  16. 42表盘直径是从哪测量_万用表测量电容容量的方
  17. Php货币计算怎么样才严谨,php怎样【货币问答】- php怎样所有答案 - 联合货币
  18. 浏览器缓存之http缓存和service worker
  19. 2020-12-22 ACM集训一(二维数组与结构体)
  20. 对拉勾网数据分析职位做数据分析

热门文章

  1. 浏览器插件-- Browser Helper Object(BHO) 一
  2. 【HTTPS、HTTP】网易新闻首页https跨协议调用http的问题
  3. 【深入理解JVM】Java类文件的基本结构
  4. Android基本程序单元Activity总结
  5. BizTalk开发系列(十九) BizTalk命名规范
  6. MySQL- 用Navicat通过隧道连接到远程数据库
  7. Running Maven project on Tomcat from Eclipse
  8. CSS相邻兄弟选择器
  9. PathRemoveFileSpec函数
  10. MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践