3

I'm trying to create a simple script to copy the filepath of a selected file (in windows explorer) to the clipboard in python. I've been looking at pyperclip and tkinter but I'm unsure how exactly to proceed.

The askopenfilename in tkinter seems promising, but I'd like to select the files outside python and then call the script through the windows context menu.


EDIT:

I want to create a script which can alter a local filepath to a network path when I copy it using the windows context menu (right click).

e.g. when right-clicking on my file C:\Users\LocalUser\test.txt in windows explorer I want to add a dropdown option to copy the filepath, but change the directory to e.g. D:\Users\LocalUser\test.txt.

I'm thinking of adding the context menu option by adding a new key in RegEdit and adding a shortcut to the python script in Computer\HKEY_CLASSES_ROOT\*\shell, but in order to do so, I need to be able to copy add the filepath to my clipboard first.

Martin
  • 353
  • 1
  • 6
  • 23
  • What have you got so far? tkinter can deal with both the dialog and pasting the file path in to the clipboard. Perhaps you can also describe your use case, I'm unsure what your end result is intended to be. – scotty3785 Jun 28 '21 at 09:37
  • So far I don't really have anything, as I can't figure out how to get the filepath from windows explorer. But please see the edit added to the original topic for a more descriptive workflow – Martin Jun 28 '21 at 10:34
  • 1
    Just create a python script which copy the passed command line argument to clipboard using `pyperclip` module. Then create the context menu item to execute the python script with the selected file as the command line argument. – acw1668 Jun 28 '21 at 13:28

2 Answers2

1

You are right, to add something in windows context menu, you will need to edit the windows registry editor.

To copy filelocation in clipboard, you can use pyperclip but this can be done using only tkinter :

from tkinter import Tk, filedialog

r = Tk()
r.withdraw()
filename = filedialog.askopenfilename()
#print(filename)
r.clipboard_clear()
r.clipboard_append(filename)
r.update() # now it stays on the clipboard after the window is closed
r.destroy()

What you can do is, when in file explorer, you right-click, then in the context menu, there will be an option (such as, "copy the file location of a file") which you can add using registry editor. Then on clicking that option, another file dialog is opened, in which the location of whichever file you select is then copied to your clipboard.


EDIT: To only add a "Copy Path" option in the context menu:

Reference

In registry editor, for files, in HKEY_CLASSES_ROOT\*\shell\Copy Path\command, and for folders, in HKEY_CLASSES_ROOT\Directory\shell\Copy Path\command, add the following command by setting the value of (Default) to

cmd.exe /c (echo.|set /p=%1) | clip

That's it, without python, using only the default command line interpreter, you can copy the full path of file/folders in windows.

Kartikeya
  • 818
  • 2
  • 6
  • 11
  • I found similar pieces of code on google, but I'd like to avoid the secondary file dialog as that kind og defeats the purpose of adding it to the context menu. It should be as simple as possible, and as other small programs uses similar workflows without the secondary dialog box, i'd imagine that it should be possible to code in Python as well. – Martin Jun 28 '21 at 11:44
  • 1
    Well in that case, you don't even require to add a python script, you can do that using only the default command-line interpreter in windows registry editor Let me edit my answer. – Kartikeya Jun 28 '21 at 13:29
  • Ah great, I'm almost there now, thanks! Is there a way to put 'cmd.exe /c (echo.|set /p=%1) | clip' into a bat file which and run it through the regedit instead? That way I can save the path to clipboard and run it through python to manipulate the string and save it to clipboard again afterwards – Martin Jun 29 '21 at 07:47
  • Hope [this link](https://stackoverflow.com/questions/9392874/bat-file-open-new-cmd-window-and-execute-a-command-in-there) helps. – Kartikeya Jun 29 '21 at 07:58
  • 1
    Through a million google searches I found that just using a "&" enables you to put more commands after each other in the regedit. It now works as intended. Thank you very much for you help! – Martin Jun 29 '21 at 11:52
0

simple_upload.html

{% extends 'base.html' %}

{% load static %}

{% block content %}
  <form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <input type="file" name="myfile">
    <button type="submit">Upload</button>
  </form>

  {% if uploaded_file_url %}
    <p>File uploaded at: <a href="{{ uploaded_file_url }}">{{ uploaded_file_url }}</a></p>
  {% endif %}

  <p><a href="{% url 'home' %}">Return to home</a></p>
{% endblock %}

views.py

from django.shortcuts import render
from django.conf import settings
from django.core.files.storage import FileSystemStorage

def simple_upload(request):
    if request.method == 'POST' and request.FILES['myfile']:
        myfile = request.FILES['myfile']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)
        return render(request, 'core/simple_upload.html', {
            'uploaded_file_url': uploaded_file_url
        })
     return render(request, 'core/simple_upload.html')

The file will be copied in the base folder, it can be copied to a specified folder by specifying the folder name

filename = fs.save('folderName/'+ myfile.name, myfile)
Merrin K
  • 1,602
  • 1
  • 16
  • 27