Question

I have a notepad file in my PC with path D:/example.txt,how can i display this file using my python code.

Was it helpful?

Solution

If you mean open the file with notepad:

Using os.startfile (only available in Windows):

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

Using subprocess.Popen or subprocess.call:

import subprocess
subprocess(['notepad', r'd:\example.txt'])

To print to console

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')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top