How to open a file in python 3

2019-09-30 06:03发布

I want to actually open it not just read it.
I tried to do something like this:

x = open("filename", "r")
abc = x.read()
x.close()

The above code just read the file, it did not actually open the file.
The file is a "bat" file

标签: python-3.x
2条回答
虎瘦雄心在
2楼-- · 2019-09-30 06:53

open creates a stream of data associated with the file. It does not initiate a file viewing software.

os.startfile('path/to/file.ext') # opens file in respective program
查看更多
够拽才男人
3楼-- · 2019-09-30 06:58

You must invoke filename.bat

Also, the explicit .read() can be removed in abc = x.read().

查看更多
登录 后发表回答