0

Hoping somebody can help me with the following. The function works just fine:

def fileOpen(filename, accessmode):
    file = open(filename, accessmode)
    for line in file:
        print(line)
    return

The filename is "open.txt"

Instead of opening notepad the results are displayed in the python console. Notepad is not opened. I know that it can be solved with this:

import os
os.system('notepad.exe ' + 'open.txt')

But I was wondering if there is a setting in visual studio to get around my problem? Any help would be greatly appreciated.

VisualStudio Community 2017 Python 3.6.6

Sparkington

Amir Shabani
  • 3,857
  • 6
  • 30
  • 67
  • 1
    So you want the `print` function to open notepad instead of **printing** it to the console? (which is its job) what's wrong with `os.system('notepad.exe' + 'open.txt')` ? – Amir Shabani Mar 13 '19 at 13:56
  • 1
    Hi there, what exactly you're trying to accomplish with this script? Right now you're reading the file and printing it's contents into console. – Ardein_ Mar 13 '19 at 13:56
  • There's no problem in this code. `open` is meant to open a file, not *execute* it – Panagiotis Kanavos Mar 13 '19 at 14:03
  • why would you expect calling `open()` to launch an arbitrary Windows text editor? It creates a file handle (opens the file for read or write access) – Corey Goldberg Mar 13 '19 at 14:07
  • 1
    A small checklist for a question: 1) here is what I want to achieve, 2) here is what I tried, 3) here is what is wrong. As of now, the question reads "I was able to print my file to console, notepad.exe does not open, what is wrong with VisualStudio setting?" – Evgeny Mar 13 '19 at 14:09

1 Answers1

1

The 'problem' is that there is no problem. You are attempting to use the print function to open notepad, not to print to the console - which is its job. This is 'solved' by using os.system() which will execute the notepad.exe program with the argument of 'open.txt'.

So I think you do not need to be looking for a solution to this problem, as there is no problem to be honest with you. You are using print to do something which is not its intended purpose, or even close to it. So is there any wonder that it doesn't work?

My suggestion to you, is to just use os.system and not to try using print for something other than its function.

Amir Shabani
  • 3,857
  • 6
  • 30
  • 67
John Smith
  • 65
  • 2
  • 11
  • Thanks John, a well formulated and substantiated answer. as you can read I am new to all this. I will be more careful in the future with my questions. Judging by the responses its easier to point out that somebody is wrong than to be constructive – Sparkington Mar 13 '19 at 14:59
  • That is good to hear. I tried to not just tell you that you are doing something wrong, but to tell you why and be constructive, so I hope you are satisfied with the answer and you will either use os.system or an equivalent method. – John Smith Mar 13 '19 at 15:32
  • I followed a microsoft MVA python course and every time they used open or print for a file it "miraculously" opened the appropriate file in notepad, excel etc and no mention of and certainly not visible an os import or os.system. So I naively assumed that there was some sort of file Association going on. Thanks again – Sparkington Mar 13 '19 at 16:25
  • That seems more understandable now, thanks for clarifying and although that may be so, it is certainly not normal behavior for print. You are welcome for the help, I am glad I could resolve your problem, if not in the intended way. – John Smith Mar 13 '19 at 16:56
  • Also, did you mean to spell your username "Sparkington" or "Spakington"? Because your username is "Spakington" and in the post it is "Sparkington". As well, not to be demanding or anything, but if you accept this answer could you please mark this answer as accepted. – John Smith Mar 13 '19 at 17:00