Skip to content
Advertisement

How to copy, cut folder from one folder to another using ctrl+c and ctrl+v

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.

JavaScript

Advertisement

Answer

For windows, you can use the Powershell command Set-Clipboard. To you can run the command use the subprocess module. Now that the file/folder is copied you can now paste it in file explorer using ctrl+v or using the right-click context menu.

To handle the paste simply use clipboard_get() provided by tkinter which will provide you with the path to the file/folder. You can then make use of shutil.copy/shutil.copytree to copy the contents from src in your application.

You can then reload the tree view to make the changes visible.

Example:

JavaScript

If you want this to work with other OS you will have to find the respective commands eg

alternatively, you can also make use of win32clipboard, 1 or you can make use of PyQt/pyslide’s QClipboard or PyGTK clipboard which provides convenient methods to do these kinds of operations

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement