0

I'm currently running a python code "Restart.py" in order to restart another python script "Tracking.py" using Shell 'restart.sh'.

The shell script launches "Tracking.py" and creates a .log file which stores all the print from "Tracking.py" which is mainly a kind of monitoring text.

Sometimes it happens that "Traking.py" crashes with a message of : "During handling of the above exception, another exception occured" which is written in the .log file.

Then, "restart.py" script reads the .log file every 20sec and when he finds a key word : "exception" it is supposed to delete .log file and then executes restart.sh (which will recreate a new .log file)

Here the code

import subprocess import os import time while True: with open("log.log",'r') as f: for line in f: if "exception" in line: os.remove('log.log') wait = 20 time.sleep(wait) subprocess.call(["sh", "./restart.sh"]) 

When it comes the crash, the script restarts well the shell script (and Tracking.py) but it doesn't delete log.log file and so keep storing all the print in the undeleted .log file.

Could you please tell me what is wrong?

1
  • 2
    I think that you should close the file before deletion. Maybe break the loop or create a flag to notice if "exception" appears in any line. Commented Feb 14, 2022 at 20:54

2 Answers 2

1

You should close the file before trying to delete it. This is explicitly mentioned in the os.remove documentation:

On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.

Just move the os.remove part out of your context manager. This is a possible solution:

while True: must_delete_file = False with open("log.log",'r') as f: for line in f: if "exception" in line: must_delete_file = True break # Not really necessary, skips the remaining iterations if must_delete_file: os.remove('log.log') wait = 20 time.sleep(wait) subprocess.call(["sh", "./restart.sh"]) 
Sign up to request clarification or add additional context in comments.

1 Comment

sorry for the late reply, I was trying different option, the log.log file still remain the same when it meets "exception" keyword
0

If you are on Windows, it is likely you won't be able to delete the file that is currently open.

Just read all the data, and have the file closed before calling os.remove.

name = 'log.log' for line in open(name).readlines(): # (*) if 'exception' in line: os.remove(name) 

(*) <- No need for a 'with block': when no references are left for the open file object, its __del__ method is called and it is closed

3 Comments

shouldn't you close the file after reading the whole content?
Python closes the file automatically when the object is destroyed. That happens when there are no references to it, which is imediatelly when "readlines" return (implementation detail, it is non buffering).
Hey, thank you, I also try your code, but it appears that log.log still remains the same when meeting "Exception" keywords, I also thought that I had to kill the shell script using : pkill -f restart.sh and then os.remove(name), it looks like it doesn't work neither

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.