#πŸ”’ How to open .py file within script

28 messages Β· Page 1 of 1 (latest)

willow drum
#

Hi! To kleep m,y request as simple as possible, I'm trying to have a python script open another python script then close itself. It doesn't need to have .read or anything. heres some things I tried and why they failed. For context, "destination" is the path of a "test.ico" file for now so I can see if it works or not. The final "destination" will be a .py file.

open(destination) # Doesn't open the file in IDLE, doesn't when I just run the program and exits withoutbeing commanded to excit
import subprocess
subprocess.run(destination) # prints error "OSError: [WinError 193] %1 is not a valid Win32 application"
subprocess.Popen(destination) # same error
import os
os.startfile(destination) # Worked once but for some reason now refuses to work
import os
os.system(destination) # refuses to work, prints no errors
exec(open(destination).read()) # works but waits for the app to close before continuing execution of current code, which I don't want as I want it to quit
exec(open(destination)) # prints error: "TypeError: exec() arg 1 must be a string, bytes or code object"
normal topazBOT
#

@willow drum

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

faint shell
#

not sure what you're trying to do -- you want the first script to start the second script running?

willow drum
faint shell
#

any reason you can't just use subprocess.run?

#

Did you say something about wanting the first script to go away before the second one finishes?

willow drum
#
import subprocess
subprocess.run(destination) # prints error "OSError: [WinError 193] %1 is not a valid Win32 application"
subprocess.Popen(destination) # same error
willow drum
faint shell
#

try this ```py
import subprocess
import sys

subprocess.run([sys.executable, destination])

willow drum
#

Tried it via IDLE but it prints no errors and does nothing but when running the .py it does this

faint shell
#

those screenshots don't seem to relate to my suggestion

#

in any case, stick another \ before the \Downloads

willow drum
faint shell
#

or better yet, just use single forward slashes

#

fix the syntax error first; you haven't even gotten to the line of code that I suggested

willow drum
#

Still seems to give the same error when I appended it to "\\Downloads\\" Ill try with forward slashes now

faint shell
#

wait, you can't "run" an .ico file.

#

What are you trying to do?

#

Are you trying to display the icon on the screen, or start a program that will edit the icon?

willow drum
#

Give me a little bit sorry, may be a while

faint shell
#

ok, so try this ```py
subprocess.run(["cmd", "/c", "start", destination])

willow drum
#

So I made a python script that just prints "hello world" and waits for 10 seconds. When I use subprocess.run([sys.executable, destination]) it opens the "hello world" script inside the first scrip which isn't what I want

willow drum
faint shell
#

πŸ’

winter surge
#

import πŸ‘€

normal topazBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.