Last active
September 8, 2022 05:28
-
-
Save MathiasBaumgartinger/1f607cbf75203bd47c9fb0713741cf15 to your computer and use it in GitHub Desktop.
WSL python script for easy opening of files and directories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Add this script to a global directory (e.g. /usr/local/bin) in your WSL | |
without the ``.py`` extension. | |
This script will automatically open any file/directory with it's default | |
Windows Application. Directories will be opened with Windows' file | |
explorer. | |
Usage: open <(dir/file)> | |
""" | |
import os | |
import sys | |
import subprocess | |
import re | |
def print_usage(): | |
print("Usage: open <(dir/file)>") | |
if len(sys.argv) != 2: | |
print_usage() | |
exit() | |
path = str(sys.argv[1]) | |
if not os.path.exists(path): | |
print("Path does not exists") | |
print_usage() | |
exit() | |
result = subprocess.run(["wslpath", "-w", path], text=True, capture_output=True) | |
if(result.returncode != 0): | |
print("Something went wrong while converting to Windows-Path") | |
printUsage() | |
windows_path = result.stdout | |
if os.path.isdir(path): | |
subprocess.run(["explorer.exe", windows_path]) | |
else: | |
os.system("cmd.exe /C start %s" % (re.escape(windows_path))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice