0

I have a Python script that needs to be crashes after 6 hours because the authorization expires. I want to write a Script that open and runs my original python script, closes it after 6 hours, and then reopens and runs it.

So basically a script that behaves like this

while true:
  run foo.py 

foo.py, obviously is my original python script. foo.py runs completely before it loops.

I tried adding an if loop in the python script that reauthorizes when authorization expires but it wouldn't reauthorize. I need to close the script and reopen to authorize.

Alec
  • 6,521
  • 7
  • 23
  • 48
  • 1
    Possible duplicate of [Calling an external command in Python](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python) Also: https://stackoverflow.com/questions/7974849/how-can-i-make-one-python-file-run-another – dani herrera Apr 26 '19 at 18:46
  • 3
    That sounds like a really bad way to handle the authorization issue. – user2357112 supports Monica Apr 26 '19 at 18:46
  • Can you not `import foo`, and write python code that calls other python code? – Kevin Wang Apr 26 '19 at 18:47
  • open(file, mode) – Bugbeeb Apr 26 '19 at 18:47
  • 1
    Possible duplicate of [How can I make one python file run another?](https://stackoverflow.com/questions/7974849/how-can-i-make-one-python-file-run-another) – Alec Apr 26 '19 at 18:48
  • If you are on linux you can use [bash script](https://en.wikibooks.org/wiki/Bash_Shell_Scripting) And if you are on Windows you can take help of [batch script](https://en.wikibooks.org/wiki/Windows_Batch_Scripting) – vaku Apr 26 '19 at 18:49
  • 1
    I heard you like Python scripts, so I put a Python script in your Python script. – joeforker Apr 26 '19 at 19:01

2 Answers2

0

Some answers may tell you to use execfile(file1.py). Don't do that.

The safest way is to simply import the other file:

import file1

or

from file1 import desired_function

Importing a file for the first time will run everything in that file not under if __name__ == '__main__':

You can then call desired_function() as many times as you want in file2.

Alec
  • 6,521
  • 7
  • 23
  • 48
0

Using a shell process:

import os

os.system('python file.py')
`
Anjan Kumar
  • 1
  • 1
  • 1