-1

How do I open a file in Python 3, like in a different program not as a text file. All solutions I found online were like this

f1 = open("something.txt", "r")
print(f1.read())
f1.close()

but that just outputs the file contents. I want to open i.e. an .exe file as an executable.

How can I do this?

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    Did you take a look at [`subprocess`](https://docs.python.org/3/library/subprocess.html) module? – bereal Oct 21 '21 at 07:50

2 Answers2

1

If you want to execute a .exe file you can do it simply like this:

import os
os.startfile("C:\Documents and Settings\flow_model\flow.exe")

You can also check subprocess

0

variant 1: start a program

import os
os.system("something.exe")

variant 2: start a program and read its output

import os
f1 = os.popen("something.exe")
print(f1.read())

variant 3: open text file in custom program (notepad.exe)

import os
s = os.system('notepad.exe something.txt')

variant 4: open text file by system, as double click in file explorer

import os
s = os.startfile('something.txt')
11_22_33
  • 116
  • 1
  • 15