如何使用python显示或显示记事本文件

时间:2014-02-15 14:00:45

标签: python-2.7 notepad

我的PC上有一个带有路径D的记事本文件:/example.txt,我可以用我的python代码显示这个文件。

1 个答案:

答案 0 :(得分:2)

如果您的意思是用记事本打开文件:

使用os.startfile(仅适用于Windows):

import os
os.startfile(r'd:\example.txt')

使用subprocess.Popensubprocess.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')