在Python终端中打开文件通常有以下几种方法:
python 文件名.py
2. 使用`python -c`命令执行文件内容:
python -c "print(open('文件路径', 'r').read())"
3. 使用`with`语句和`open`函数打开文件,并自动关闭文件:
with open('文件路径', 'r', encoding='utf-8') as f:
content = f.read()
print(content)
4. 使用`codecs`或`io`模块打开文件:
import codecs
with codecs.open('文件路径', 'r', encoding='utf-8') as f:
content = f.read()
print(content)
或者
import io
with io.open('文件路径', 'r', encoding='utf-8') as f:
content = f.read()
print(content)
请确保将`文件路径`替换为实际文件的路径。这些方法适用于Python 2和Python 3。