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 fo
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.