So I'm running a loop through a directory where an external script, run by subprocess.Popen, is moving through the directory and performing calculations on each file. The external script is a bit unstable and occasionally freezes when it runs into files that it doesn't know how to handle. Is there a way to add a timeout function to subprocess.Popen so that I can skip over that file and move on to the next?
EDIT: Here's my loop:
def automate():
os.chdir("/home/mlts/dir")
working_dir = b"/home/mts/dir"
for filename in os.listdir(working_dir):
if filename.endswith(".MTS"):
try:
print("Performing calculations on {filename!r}...".format(**vars()))
try:
os.remove("mts_tbl.txt")
except OSError:
pass
time.sleep(3)
p = Popen(["run_command.command", "-f", "a"], cwd=working_dir, stdin=PIPE)
p.communicate(input=b"\n".join([b"1", str(filename), b"8", b"alloy-liquid", b"0", b"x", b"5", b"4", b"-1.6", b"4", b"1", b"0"]))