我读到有一种方法可以用 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/