0

How would I go about opening a file in Python by referring to a text file. EXAMPLE:

    f.open('openthis.txt')

Then I would have openthis.txt in the same folder that would say:

C:\Folder\myprogram.exe

Therefore the code opens up myprogram.exe from the directory

I want to do this so the code is easily changable, instead of having to edit it in IDLE every time I want to change the file I open.

1 Answer 1

1

You want the subprocess module. Specifically, you'd do something like:

import subprocess

with open("inputfile", "rb") as f:
    subprocess.call(f.read())
Sign up to request clarification or add additional context in comments.

4 Comments

Would I need an 'import subprocess' or something along those lines?
yup, I'll edit the code to make that clear for future readers
@Kyle: not sure about the "rb"... isn't 'inputfile' the text file containing the name of the executable? so why use binary mode?
@isedev: probably not necessary, just habit on my part :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.