Can anyone help enhance this Python script executed on Download?


I knocked this up a while back but I don’t know Python (or any code really), it was more just guessing. The idea is to delete the file the torrent has downloaded and create a symlink to the file that Sonarr has imported, so you can permaseed.

What I want it to do is when it tries to delete the file, retry indefinitely until it succeeds as the Windows file locks will prevent it from happening if it’s still uploading. This cannot include if the error is “File does not exist” as if that’s the case it should move on as it’s already done it and can move on.

import logging
import os
import sys
import time

# Set up the log file

# Get the paths from the environment variables
permpath = os.environ.get('Sonarr_EpisodeFile_Path')
linkpath = os.environ.get('Sonarr_EpisodeFile_SourcePath')

# Sleep 1 minute to avoid the surge

# Delete the original file
except OSError:

# Create the symlink
os.symlink(permpath, linkpath)

# Log it if level is set to debug
logging.debug('Created link from symlink location: [%s] to permanent location: [%s]', linkpath, permpath)