Python Forum
How can I paste an entire file from clipboard to a folder?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I paste an entire file from clipboard to a folder?
#4
You can copy text from clipboard with Tkinter or Pyperclip.
Tkinter is shipped together with Python.

What you could not do, is getting raw bytes from a copied image to clipboard.
Tkinter doesn't seem to support it.

If you copy a File to your Clipbaord, you'll get a String back.
This code should work and return a Path object, if the Path was valid.
Otherwise it return None.

from pathlib import Path
from tkinter import Tk, TclError
 
 
def get_clipboard_as_path():
    root = Tk()
    root.withdraw()
    try:
        content = root.selection_get(selection="CLIPBOARD")
    except TclError:
        return None
    finally:
        root.destroy()
 
    file = Path(content)
   
    try:
        if file.exists():
            return file
    except OSError:
        pass
   
    return None
 
 
# get the path
my_path = get_clipboard_as_path()
# my_path could be None
if my_path:
    print(f'The path is {my_path}')
    # if my_path is a directory, find all python files
    if my_path.is_dir():
        print('The path is a directory')
        for file in my_path.glob('*.py'):
            print(file)
    else:
        print('The path is a file')
        with my_path.open() as fd:
            header = fd.readline()
            print('First line:')
            print(header)
else:
    print('It was not a Path, maybe a image or something else')
If win32clipboard has a better support for clipboard, you should use this.
Tkinter is very limited.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: How can I paste an entire file from clipboard to a folder? - by DeaD_EyE - Feb-08-2020, 01:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 526 Feb-07-2024, 12:24 PM
Last Post: Viento
  Make entire script run again every 45 mo NDillard 0 367 Jan-23-2024, 09:40 PM
Last Post: NDillard
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 663 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  What script to paste folders thenewcoder 1 723 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  non-latin characters in console from clipboard Johanson 3 776 Oct-26-2023, 10:10 PM
Last Post: deanhystad
  Reading a file name fron a folder on my desktop Fiona 4 1,024 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Please help me [copy and paste file from src to dst] midomarc 2 1,070 Nov-24-2022, 10:13 PM
Last Post: midomarc
  Function not executing each file in folder mathew_31 9 2,418 Aug-22-2022, 08:40 PM
Last Post: deanhystad
  saving and loading text from the clipboard with python program MaartenRo 2 1,743 Jan-22-2022, 05:04 AM
Last Post: MaartenRo
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,282 Jan-07-2022, 04:55 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020