我试图创建一个简单的脚本,以便将选定文件的文件路径(在windows资源管理器中)复制到python中的剪贴板。我一直在看pyperclip和tkinter,但我不知道该如何进行。
askopenfilename in tkinter似乎很有希望,但我想选择python之外的文件,然后通过windows上下文菜单调用脚本。
编辑:
我想要创建一个脚本,当我使用windows上下文菜单复制它时,它可以将本地文件路径更改为网络路径(右击)。
例如,在windows资源管理器中右键单击我的文件C:\Users\LocalUser\test.txt时,我希望添加一个下拉选项来复制文件路径,但将目录更改为D:\Users\LocalUser\test.txt。
我正在考虑通过在RegEdit中添加一个新键并在Computer\HKEY_CLASSES_ROOT\*\shell中为python脚本添加一个快捷方式来添加上下文菜单选项,但要做到这一点,我需要首先将文件路径复制到剪贴板中。
发布于 2021-06-28 10:40:26
您是对的,要在windows上下文菜单中添加某些内容,您需要编辑windows注册表编辑器。
要在剪贴板中复制文件定位,可以使用pyperclip,但只能使用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()您可以做的是,当您在文件资源管理器中右键单击,然后在上下文菜单中,将有一个选项(例如,“复制文件位置的文件”),您可以添加使用注册表编辑器。然后单击该选项,将打开另一个file dialog,然后将所选文件的位置复制到剪贴板上。
编辑:只在上下文菜单中添加“复制路径”选项:
在注册表编辑器中,对于文件,在HKEY_CLASSES_ROOT\*\shell\Copy Path\command中,对于文件夹,在HKEY_CLASSES_ROOT\Directory\shell\Copy Path\command中,通过将(默认值)的值设置为
cmd.exe /c (echo.|set /p=%1) | clip就是这样,没有python,只使用默认的命令行解释器,您就可以在windows中复制文件/文件夹的完整路径。
发布于 2021-11-22 07:46:04
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')该文件将在基文件夹中复制,可以通过指定文件夹名将其复制到指定的文件夹中。
filename = fs.save('folderName/'+ myfile.name, myfile)https://stackoverflow.com/questions/68159696
复制相似问题