Skip to content

Instantly share code, notes, and snippets.

@JorianWoltjer
Last active July 16, 2022 13:37
Show Gist options
  • Save JorianWoltjer/cc4ed7415b665d35e2d010cd2c04c8a6 to your computer and use it in GitHub Desktop.
Save JorianWoltjer/cc4ed7415b665d35e2d010cd2c04c8a6 to your computer and use it in GitHub Desktop.
Python script that allows you to drag and drop Windows files into a WSL terminal, and copy them to the working directory
#!/usr/bin/python3
from blessed import Terminal
import subprocess
import shlex
term = Terminal()
def read_path():
# Read input from drag
full = ""
with term.cbreak(): # set keys to be read immediately
# wait and read one character
full += term.inkey()
while key := term.inkey(timeout=0.1): # Read rest of characters
full += key
if len(full) <= 1:
if full == "\x1b": # ESC
exit()
print("Hint: Drag the file to the terminal with your mouse")
return read_path()
return full
if __name__ == "__main__":
full = read_path()
for path in shlex.split(full, posix=False):
path = path.replace("\\\\", "\\").replace("\"", "").replace("\'", "")
# Convert to WSL path (Not needed for newer versions)
if path.startswith("/"):
wslpath = path
else:
wslpath = subprocess.check_output(["wslpath", path]).decode("utf-8").strip()
# Copy file
print(f'Copying "{path}"... ', end="")
if subprocess.run(["cp", "-r", wslpath, ".", "--no-preserve=mode"]).returncode == 0:
print("Done!")
else:
print("ERROR!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment