How do I bind a python script to a file, and it runs on the background when the file is opened?
Lets say I want to bind a file to a photo and then the photo is closed or open the script runs
How do I bind a python script to a file, and it runs on the background when the file is opened?
Lets say I want to bind a file to a photo and then the photo is closed or open the script runs
In Windows (for example), you could associate the file type (not one particular file, but all files with a certain extension) with the python script, and then launch the photo viewer (with the filename as argument, so that the application opens this file) from within the python script. This way, both your script and the "main" application will be run when opening the file type.
BTW: In your script you could test the filename for certain patterns or substrings, to do different actions with different files of this file type...
This solution may not be as smooth as you'd hope, but it probably could do as a "workaround".
Check this question (rather it's answers), and this answer,
And in-case you don't want to do so much reading (though you should),
What you might need is to use:
os.startfile()
on Windowssubprocess.call(['open',..])
on Macsubprocess.call(['xdg-open', ...])
on *nixWhich come to something like:
import platform, os, subprocess
#....
def nix_open(filename):
pass # I don't know this one, you can research
def win_open(filename):
return os.startfile(filename)
def mac_open(filename):
return subprocess.call(['open',filename])
def handle_open(filename):
s_name = platform.system()
if s_name in ('Linux','Unix'):
return nix_open(filename)
elif s_name == 'Windows':
return win_open(filename)
elif s_name == 'Darwin':
return mac_open(filename)
# ...
else:
raise EnvironmentError, 'OS not supported'