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?
#5
(Feb-08-2020, 01:03 PM)DeaD_EyE Wrote: 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.

Nice, thanks for the suggestions!
I can't believe I couldn't find this stuff on the internet more easily.
Cheers!
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete file with read-only permission, but write permission to parent folder cubei 6 22,465 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  Copy Paste excel files based on the first letters of the file name Viento 2 609 Feb-07-2024, 12:24 PM
Last Post: Viento
  Make entire script run again every 45 mo NDillard 0 394 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 745 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  What script to paste folders thenewcoder 1 788 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  non-latin characters in console from clipboard Johanson 3 837 Oct-26-2023, 10:10 PM
Last Post: deanhystad
  Reading a file name fron a folder on my desktop Fiona 4 1,109 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Please help me [copy and paste file from src to dst] midomarc 2 1,129 Nov-24-2022, 10:13 PM
Last Post: midomarc
  Function not executing each file in folder mathew_31 9 2,553 Aug-22-2022, 08:40 PM
Last Post: deanhystad
  saving and loading text from the clipboard with python program MaartenRo 2 1,823 Jan-22-2022, 05:04 AM
Last Post: MaartenRo

Forum Jump:

User Panel Messages

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