Python Forum
Is it possible to have a script call itself? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Is it possible to have a script call itself? (/thread-1929.html)

Pages: 1 2


Opening a .py file with a python script [HELP] - treycoxiii - Feb-04-2017

Hi everyone I am trying to write a very very simple python script that opens up a .py file on my desktop using import os

this is what the script looks like in the shell:

import os
os.startfile("c:\Users\Trey\Desktop\Vote.py")


The problem with this scrip is it will find the file and open it, but it will close as soon as it opens.
This idea is that this scrip will find the vote.py file and open it so it will run. Whenever it does find it, it wont run b/c it closes as soon as it opens.

Please help! Thanks!


Opening a .py file with a python script [HELP] - treycoxiii - Feb-04-2017

Hi everyone I am trying to write a very very simple python script that opens up a .py file on my desktop using import os

this is what the script looks like in the shell:

import os
os.startfile("c:\Users\Trey\Desktop\Vote.py")


The problem with this scrip is it will find the file and open it, but it will close as soon as it opens.
This idea is that this scrip will find the vote.py file and open it so it will run. Whenever it does find it, it wont run b/c it closes as soon as it opens.

Please help! Thanks!


RE: Opening a .py file with a python script [HELP] - Larz60+ - Feb-04-2017

just use
python Vote.py



RE: Opening a .py file with a python script [HELP] - Larz60+ - Feb-04-2017

Don't post duplicate posts!


RE: Opening a .py file with a python script [HELP] - treycoxiii - Feb-04-2017

Hey thanks for the help! Sorry about the duplicate post. I have one more question, after the program has executed everything how do i get it to exit itself out, or quit the program?

(Feb-04-2017, 09:22 PM)Larz60+ Wrote: just use
python Vote.py

what i mean by my earlier comment is how would i force the program to quit


Is it possible to have a script call itself? - treycoxiii - Feb-04-2017

I want my script to execute, call itself, then quit. The idea is to have the program execute, call itself to re-open the file, then the old program quits and the newly opened program executes and repeats the process. Is this possible?

For example:

I open example.py
the code executes
at the end example.py is called within the script to open it again
quit()


The purpose is to have example.py loop itself continuously without me having to manually close and open it again and again.
**Something to keep in mind is that the program MUST close and re-open, not just loop the code inside.


RE: Opening a .py file with a python script [HELP] - nilamo - Feb-04-2017

I'm confused. Why start a script if you want to force that script to end? Why not... just not start it at all?


RE: Is it possible to have a script call itself? - ichabod801 - Feb-04-2017

I don't think so. You can run the file from within the file, but I don't see how you could close the outer program before doing so. You would have to schedule the system to rerun the code. It all just sounds like a very bad idea. Why must the program close and reopen?


RE: Is it possible to have a script call itself? - nilamo - Feb-04-2017

Your three different threads have been merged into this one. Those of us who are here to help, look at all the subforums, so there's no reason to post more than once.


RE: Is it possible to have a script call itself? - treycoxiii - Feb-04-2017

Not sure how all these posts got combined/merged, but I figured out my own question for all you wondering minds.

before i ask example1.py to exit I can use os.startfile to open example2.py which also uses os.startfile to open back up example1.py. Put simply, example1.py will open, execute, and close continually until i stop it! 

I am new to these posts so thanks for the instructional criticism!!!!! :)))))