python - 关闭文件 python

标签 python file

我读到有一种方法可以用 python 打开文件..

os.startfile('文件.exe')

有没有办法在打开同一个文件时关闭它?

提前谢谢您!

最佳答案

来自 os.startfile() doc :

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.

所以,基本上,不,没有办法关闭使用 startfile 打开的文件。

从问题中不清楚您是要启动文件,还是打开它(用于读/写)。

如果您想启动一个进程,子进程是运行其他进程并通过子 shell 控制它们(包括杀死它们)的更好选择。

如果你想打开一个文件进行读/写,那么 open()开始是一个不错的选择。

关于python - 关闭文件 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16025296/

相关文章:

python - 如何处理 Airflow 中的DAG lib?

python - 在 uwsgi chain-raload 期间预热 django 应用程序

python - Numpy:如何避免舍入错误

python - WSGIPythonPath 不工作

使用 fscanf 计算文本文件中有多少个整数

java - 找到txt文件,为每个文件创建一个空文件,然后删除原始文件

android - 如何将我的文件从一个目录复制到另一个目录?

python - 如何连接 pandas MultiIndex DataFrame 中的列值?

c++ - 在 C Linux 中通过套接字发送图像 (JPEG)

bash - 如何检查 bash 脚本中文件名的扩展名?