Cross-Platform Python: Select and show a file in its folder with Python

in #programming8 years ago

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)

You can find the full code here in this Gist also

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.083
BTC 60791.27
ETH 1560.80
USDT 1.00
SBD 0.47