Cross-Platform Python: Select and show a file in its folder with Python
This is a quick Python snippet for my image viewer application.
One of the things I need to do is selectively locate the file I am viewing so I can drag and drop it.
While my main desktop is Linux, ideally it would work across platforms ...
(Windows requires C:\\Users\\Chris style file paths)
import sys
import subprocess
def select_file(path):
if sys.platform == 'darwin':
subprocess.run(['open', '-R', path])
elif sys.platform == 'linux':
subprocess.run(['nautilus', path])
else:
command = 'C:\\windows\\explorer /select,"{}"'.format(path)
print(command)
subprocess.run([command])
print("{}: {}".format(sys.platform, path))
file = '/home/pi/_logo_.png'
select_file(file)