我的PC中有一个记事本文件,路径为D:/example.txt,如何使用我的python代码显示此文件。
如果您的意思是使用记事本打开文件:
使用 os.startfile
(仅在 Windows 中可用):
import os
os.startfile(r'd:example.txt')
使用subprocess.Popen
或subprocess.call
:
import subprocess
subprocess(['notepad', r'd:example.txt'])
打印到控制台
import sys
with open(r'D:example.txt') as f:
sys.stdout.writelines(f)
with open(r'D:example.txt') as f:
for line in f:
print line.rstrip('n')