How do I close a file opened using os.startfile(), Python 3.6

前端 未结 3 517
青春惊慌失措
青春惊慌失措 2021-01-07 13:35

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

3条回答
  •  旧时难觅i
    2021-01-07 14:22

    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.

提交回复
热议问题