关闭文件python [英] Closing a file python

查看:70
本文介绍了关闭文件python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've read that there is a way to open a file in python with..

os.startfile('file.exe')

os.startfile('file.exe')

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

is there a way to close the same file when open?

提前谢谢!

推荐答案

来自

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.

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

So, basically, no, there isn't a way to close a file opened with startfile.

目前尚不清楚您是要启动文件还是要打开文件(用于读/写).

It isn't clear from the question is whether you want to launch a file, or to open it (for reading/writing).

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

If you want to launch a process, subprocess is a better candidate for running other processes and controlling them through a subshell (including killing them.)

如果您要打开文件进行读/写,请 open() 是一个很好的选择.

If you want to open a file for read/write, then open() would be a good choice to start with.

这篇关于关闭文件python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆