Skip to content

Instantly share code, notes, and snippets.

@nezuppo
Created February 8, 2024 11:37
Show Gist options
  • Save nezuppo/3298a8dacfb6a8afc21f4a3abd2adede to your computer and use it in GitHub Desktop.
Save nezuppo/3298a8dacfb6a8afc21f4a3abd2adede to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from pathlib import (
PurePosixPath,
PureWindowsPath,
)
def get_wslpath(winpath):
purewinpath = PureWindowsPath(winpath)
winpath_parts = purewinpath.parts
if winpath_parts[0].endswith(':\\'):
assert len(winpath_parts[0]) == 3, winpath_parts
assert winpath_parts[0][0].isalpha(), winpath_parts
pureposixpath = PurePosixPath('/mnt', winpath_parts[0][0].lower(), *winpath_parts[1:])
else:
pureposixpath = PurePosixPath(*winpath_parts)
wslpath = str(pureposixpath)
return wslpath
if __name__ == '__main__':
# example
winpath = 'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe'
wslpath = get_wslpath(winpath)
print()
print('winpath:', winpath)
print('wslpath:', wslpath)
from subprocess import check_call
cmd = ['wsl.exe', 'ls', '-la', wslpath]
print()
check_call(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment