Skip to content

Instantly share code, notes, and snippets.

@MathiasBaumgartinger
Last active September 8, 2022 05:28
Show Gist options
  • Save MathiasBaumgartinger/1f607cbf75203bd47c9fb0713741cf15 to your computer and use it in GitHub Desktop.
Save MathiasBaumgartinger/1f607cbf75203bd47c9fb0713741cf15 to your computer and use it in GitHub Desktop.
WSL python script for easy opening of files and directories
#!/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)))
@DavidPgl
Copy link

Nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment