My title can look a little ambiguous, so here is an explanation.
Professional IDE like Pycharm
or Visual Studio Code
allow copying the folder, navigating to a specific directory and pasting it there. I would also like to implement that.
But in my case, shutil.copytree needs 2 arguments - source folder and destination folder.
So is there any way that one can copy a folder, navigate through the explorer, click paste or press ctrl+v
and the folder will be copied or pasted there, unlike shutil.copytree
where the user already need to provide the path?
Currently, I have a code that will copy the folder name to the clipboard.
import os
import tkinter as tk
import tkinter.ttk as ttk
import clipboard
class App(tk.Frame):
def __init__(self, master, path):
tk.Frame.__init__(self, master)
self.tree = ttk.Treeview(self)
ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
xsb = ttk.Scrollbar(self, orient='horizontal', command=self.tree.xview)
self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)
self.tree.heading('#0', text=path, anchor='w')
abspath = os.path.abspath(path)
root_node = self.tree.insert('', 'end', text=abspath, open=True)
self.process_directory(root_node, abspath)
self.tree.bind("<Control-c>",self.copy_to_clipboard)
self.tree.grid(row=0, column=0)
ysb.grid(row=0, column=1, sticky='ns')
xsb.grid(row=1, column=0, sticky='ew')
self.grid()
def copy_to_clipboard(self,event,*args):
item = self.tree.identify_row(event.y)
clipboard.copy(self.tree.item(item,"text"))
def process_directory(self, parent, path):
try:
for p in os.listdir(path):
abspath = os.path.join(path, p)
isdir = os.path.isdir(abspath)
oid = self.tree.insert(parent, 'end', text=p, open=False)
if isdir:
self.process_directory(oid, abspath)
except PermissionError:
pass
root = tk.Tk()
path_to_my_project = 'C:\\Users\\91996\\Documents'
app = App(root, path=path_to_my_project)
app.mainloop()
from How to copy, cut folder from one folder to another using ctrl+c and ctrl+v
No comments:
Post a Comment