Python Forum
Is it possible to have a script call itself?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it possible to have a script call itself?
#1
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!
Reply
#2
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!
Reply
#3
just use
python Vote.py
Reply
#4
Don't post duplicate posts!
Reply
#5
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
Reply
#6
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.
Reply
#7
I'm confused. Why start a script if you want to force that script to end? Why not... just not start it at all?
Reply
#8
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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
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.
Reply
#10
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!!!!! :)))))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 734 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Call a bash script from within a Python programme Pedroski55 6 2,473 Dec-06-2021, 01:53 PM
Last Post: DeaD_EyE
  Using VBA to Call a Python script causes error in pyodbc connector pcarra 1 2,834 Jun-11-2019, 04:14 PM
Last Post: pcarra
  How to call a variable declared in a function in another python script lravikumarvsp 2 2,942 Jun-04-2018, 08:45 AM
Last Post: Larz60+
  How to call one python script and use its output in another python script lravikumarvsp 3 32,428 May-16-2018, 02:08 AM
Last Post: lravikumarvsp
  Python cmd function call from script John_O 10 9,833 Feb-18-2017, 04:45 PM
Last Post: John_O
  Windows: python script call over ssh -> output jb7731 2 5,373 Feb-08-2017, 01:13 PM
Last Post: jb7731

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020